title: R fast cbind matrix using Rcpp
body: <p><code>cbind</code> in R is relatively time consuming in repeated calls, but it also is powerful for various data types.
  I have written code that is 3X faster than <code>cbind</code> when binding two matrices. But <code>bind_cols</code> in <code>dplyr</code> package is merely 100X faster than <code>cbind</code>. It is only a pity that it cannot take matrix as input. Can someone make the code below more fast. Also, how do I fast bind sparse matrix? Here is the code I used:</p>
  
  <pre><code>require( Rcpp )
  
  func &lt;- 'NumericMatrix mmult(NumericMatrix a,NumericMatrix b) {
      //the colnumber of first matrix
      int acoln=a.ncol();
      //the colnumber of second matrix
      int bcoln=b.ncol();
      //build a new matrix, the dim is a.nrow() and acoln+bcoln
      NumericMatrix out(a.nrow(),acoln+bcoln) ;
      for (int j = 0; j &lt; acoln + bcoln; j++) {
          if (j &lt; acoln) {
              out(_,j) = a(_,j);
          } else {
              //put the context in the second matrix to the new matrix
              out(_,j) = b(_,j-acoln);
          }
      }
      return out ;
  }'
  
  a &lt;- matrix(rep(1,2000*100),2000)
  b &lt;- matrix(rep(2,2000*10),2000)
  
  cppFunction(func)
  
  system.time(for (i in seq(1,800)) {mmult(a,b)})
  system.time(for (i in seq(1,800)) {cbind(a,b)})
  identical(mmult(a,b),cbind(a,b))
  </code></pre>