Why sampling matrix row is very slow? : <p>I tried to do some bootstrapping and calculate <code>colMeans</code>, naturally I chose matrix to store data, however, it is very slow in sampling:</p>

<pre><code>m[sample(n,replace=TRUE),]
</code></pre>

<p>It turns out <code>data.table</code> is the fastest.</p>

<pre><code>require(microbenchmark)
require(data.table)
n = 2000
nc = 8000
m = matrix(1:(n*nc) ,nrow = n)
DF = as.data.frame(m)
DT = as.data.table(m)

s=sample(n, replace=TRUE)
microbenchmark(m[s,], DF[s,],DT[s,])

# Unit: milliseconds
    # expr      min       lq     mean   median       uq      max neval
  # m[s, ] 371.9271 402.3542 421.7907 420.8446 437.8251 506.1788   100
 # DF[s, ] 182.3189 199.0865 218.0746 213.9451 231.1518 409.8625   100
 # DT[s, ] 129.8225 139.1977 156.9506 150.4321 164.3104 254.2048   100
</code></pre>

<p>Why is sampling matrix much slower than the other two?</p>
