Consider the following matrix,
nc <- 5000
nr <- 1024
m <- matrix(rnorm(nc*nr), ncol=nc)
I wish to take the difference between the rowMeans of two groups of identical size taken at random in this matrix.
n <- 1000 # group size
system.time(replicate(100, {
ind1 <- sample(seq.int(nc), n)
ind2 <- sample(seq.int(nc), n)
rowMeans(m[, ind1]) - rowMeans(m[, ind2])
}))
It is quite slow, unfortunately I didn’t understand the output of Rprof (it seemed most of the time was spent on is.data.frame??)
Suggestions for something more efficient?
I have contemplated the following:
-
Rcpp: from my online readings I believe R’s rowMeans is quite efficient, so it’s not clear it would help at this step. I’d like to be convinced of where the bottleneck really is first, maybe my whole design is suboptimal. If most of the time is spent in making copies for each of the smaller matrices, would Rcpp perform better? -
updating to R-devel, there seems to be a new
.rowMeansfunction more efficient yet. Has anyone tried it?
Thanks.
Each
rowSums()call on a subset of columns frommcan be seen as the matrix multiplication betweenmand a vector of0or1indicating the selected columns. If you juxtapose all those vectors, you end up with a multiplication between two matrices (which is much more efficient):