Re: [R] Vectorize rearrangement within each column

From: Roberto Osorio <roboso_at_gmail.com>
Date: Fri 19 Jan 2007 - 19:17:32 GMT

Thanks for the solutions. Here are some time tests for ma and idx being 100 X 100,000. The machine is a 2.16 GHz Intel MacBook Pro with 2 GB memory.

ma <- matrix(rnorm(1e7), nr = 100) # 100 X 100,000 idx <- matrix(round( runif(1e7, 1, 100) ), nr = 100)

# Original:

system.time( {

mb <- ma;
for (j in 1:1e5) mb[,j] <- ma[idx[j],j] } )
[1] 1.354 0.087 1.435 0.000 0.000

# Prof. Venables' version:

system.time( mb[] <- as.vector(ma)[as.vector(idx +

outer(rep(nrow(ma), nrow(ma)), 1:ncol(ma)-1, '*'))] ) [1] 0.885 0.857 2.262 0.000 0.000

# Patrick Burns' version:

system.time( {

mb <- ma[cbind(as.vector(idx), as.vector(col(idx)))];     dim(mb) <- dim(ma)
} )
[1] 1.672 0.615 2.277 0.000 0.000

# Gabor Grothendieck's version led to some memory handling issue. I stepped one order of magnitude down in the number of columns but it's still very slow.

> ma <- matrix(rnorm(1e6), nr = 100) # 100 X 10,000
> idx = matrix(round( runif(1e6, 1, 100) ), nr = 100)
> system.time( as.matrix(mapply("[", as.data.frame(ma), as.data.frame(idx))) )
[1] 2.060 0.133 2.768 0.000 0.000

So, Prof. Venables' solution is the fastest. In view of only moderate time savings, I will take his advice and keep the original loop for code clarity.

Roberto Osorio

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat Jan 20 06:23:10 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 19 Jan 2007 - 19:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.