FWIW

R's internal representation of matrixes places *column* elements in  
adjacent memory locations.  Thus some operations can be considerably  
faster on columns than on rows.  For this reason, I've occasionally  
found it useful to transpose the matrix, then operate on columns  
instead of on rows.

-Greg



On Apr 3, 2008, at 4:28PM , Houston Gilbert wrote:

> There is also the function rowMeans(), which is also optimized for  
> doing
> these operations.
>
> Also, depending on the size of the problem, I have also found that  
> applying
> a function over rows can sometimes be slow, slower than a for loop  
> even.
> You may want to try a test case where you use a for loop with  
> preallocated
> memory rather than rbinding or writing over stuff within the loop  
> (not sure
> what you were doing before), but something like:
>
> out.matrix <- matrix(0,nrow=500,ncol=10000)
> for(i in 1:10000){
> out.matrix[,i] <- your_function(args...)
> }
> Houston Gilbert
>
> Doctoral Student in Biostatistics
> University of California, Berkeley
> www.stat.berkeley.edu/~houston
>
>
> On Thu, Apr 3, 2008 at 12:57 PM, Sean Davis <sdavis2@mail.nih.gov>  
> wrote:
>
> > On Thu, Apr 3, 2008 at 3:41 PM, Matthew R. Helmus  
> <mrhelmus@wisc.edu>
> > wrote:
> > > Hi Sean,
> > >  Thanks for your reply. I have a large data matrix (500x50) of  
> values
> > and I
> > > need to:
> > >  1) randomize
> > >  2) for each row apply a function
> > >  3) calculate the mean of the outputs of the function across  
> the rows
> > >  4) store this mean
> > >  5) then repeat the loop 10,000 times to produce a vector of  
> 10,000
> > random
> > > means.
> > >
> > >  I have used the following code, apply(X, 1, sample), to make a
> > randomized
> > > matrix maintaining row sums, but the for loop runs a bit slow that
> > creates
> > > each random matrix. I have not developed or found code that  
> maintains
> > both
> > > row and sum totals while randomizing.
> >
> > > a <- matrix(rnorm(500*50),nc=50,nr=500)
> > > for(i in 1:100) {b <- apply(a,1,sample)}
> > > system.time(for(i in 1:100) {b <- apply(a,1,sample)})
> >   user  system elapsed
> >  1.164   0.008   1.170
> > > system.time(for(i in 1:100) {b <- a[,sample(ncol 
> (a),replace=FALSE)]})
> >   user  system elapsed
> >  0.024   0.000   0.022
> >
> > So, at least the randomization code can be quite a bit more  
> efficient
> > than your "apply(X,1,sample)". For 10000 replicates, it runs is just
> > over 2 seconds.  Give this a try and then you may want to let us  
> know
> > more details if you are still having problems.
> >
> > Sean
> >
> > _______________________________________________
> > Bioconductor mailing list
> > Bioconductor@stat.math.ethz.ch
> > https://stat.ethz.ch/mailman/listinfo/bioconductor
> > Search the archives:
> > http://news.gmane.org/gmane.science.biology.informatics.conductor
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor@stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/ 
> gmane.science.biology.informatics.conductor
>

Gregory R. Warnes, Ph.D.
Associate Professor
Center for Biodefence Immune Modeling
    and
Department of Biostatistics and Computational Biology
University of Rochester





	[[alternative HTML version deleted]]

