[R] Improve a browse through list items - Transform a loop to apply-able function.

Klint Gore kgore4 at une.edu.au
Wed Dec 14 02:34:08 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Robin Cura
> Sent: Tuesday December 13, 2011 3:16 AM
> 
> I'm currently trying to convert a slow and ugly script I made, so that
> it's
> faster and can be computed on a computer grid with the multicore
> package.
> My problem is that I don't see how to turn some loops into an "apply-
> able"
> function.
> 
> 
> Here's a example script :
> 
> a <- b <- c <- d <- result <- matrix(nrow=3, ncol=3)
> a[] <- sample.int(n=100,size=9,replace=TRUE)
> b[] <- sample.int(n=100,size=9,replace=TRUE)
> c[] <- sample.int(n=100,size=9,replace=TRUE)
> d[] <- sample.int(n=100,size=9,replace=TRUE)
> result[] <- NA
> mylist <- list(a,b,c,d)
> 
> for (row in 1:3)
> {
>   for (col in 1:3)
>   {
>     tmpList <- log(mylist[[1]][row, col])
>     for (listitem in 2:4)
>     {
>       tmpList <- c(tmpList, log(mylist[[listitem]][row, col]))
>     }
>     result[row, col] <- sd(tmpList)
>   }
> }
> 
> Considering I have to look at the same cell in each dataframe, I don't
> understand how I could turn this into a function, considering I need
> the
> row and column number to iterate.
> 

How about something along the line of 

library(foreach)
library(doMC)
registerDoMC()   

# larger matrix for timing test
a <- b <- c <- d <- result <- matrix(nrow=1000, ncol=1000)
a[] <- sample.int(n=100,size=1000000,replace=TRUE)
b[] <- sample.int(n=100,size=1000000,replace=TRUE)
c[] <- sample.int(n=100,size=1000000,replace=TRUE)
d[] <- sample.int(n=100,size=1000000,replace=TRUE)
result[] <- NA
mylist <- list(a,b,c,d)
nrows=nrow(a)
ncols=ncol(a)

system.time(
{
  result<-foreach (row=1:nrows, .combine=rbind) %dopar%
  {
    thisrow=vector()

    for (col in 1:ncols)
    {
      tmpList <- log(mylist[[1]][row, col])
      for (listitem in 2:length(mylist))
      {
        tmpList <- c(tmpList, log(mylist[[listitem]][row, col]))
      }
      thisrow <- cbind(thisrow,sd(tmpList))
    }
    thisrow
  }
}
)

Example as written with the larger matrix
   user  system elapsed 
 62.660   0.000  62.722

%do%
   user  system elapsed 
 66.910   0.020  66.979 

%dopar%
   user  system elapsed 
 71.390   4.840   3.402


Klint.



More information about the R-help mailing list