Vectorization in R

I am trying to arrange each row in a matrix with multiple columns and multiple rows. Does it exist in R? More specifically, let us set our seed to 10 and make an example matrix:

set.seed(10) example.matrix = replicate(12,runif(500000)) 

To order example.matrix, I would,

 ordered.example = apply(example.matrix,1,order) 

But it is very slow, and I would like something faster. As an analogy

 rowSums(example.matrix) 

Preferably

 apply(example.matrix,1,sum) 

Great importance.

+7
source share
2 answers

Here is a way to speed it up 10 times. It is specially adapted to your example and depending on what your real data are, how this method may or may not work.

The idea is to add 0 to the first row, from 1 to the second, etc., then collapse it into a vector, sort it, and then recompile it into a matrix:

 N = 12; M = 500000; d = replicate(N,runif(M)) system.time(d1<-t(apply(d, 1, order))) # user system elapsed # 11.26 0.06 11.34 system.time(d2<-matrix(order(as.vector(t(matrix(as.vector(d) + 0:(M-1), nrow = M)))) - rep(0:(M-1), each = N)*N, nrow = M, byrow = T)) # user system elapsed # 1.39 0.14 1.53 # Note: for some reason identical() fails, but the two are in fact the same sum(abs(d1-d2)) # 0 
+3
source

This is slightly faster (the key bit is equal to order(row(em), em) ):

 set.seed(10) em <- replicate(12,runif(500000)) system.time(a <- matrix(em[order(row(em), em)], nrow=nrow(em), byrow=TRUE)) # user system elapsed # 5.36 0.12 5.80 set.seed(10) example.matrix <- replicate(12,runif(500000)) system.time(ordered.example <- apply(example.matrix,1,order)) # user system elapsed # 13.36 0.09 15.52 identical(a, ordered.example) # [1] FALSE 
+3
source

All Articles