[R] more efficient sum of matrix columns

Jonck van der Kogel jonck at vanderkogel.net
Sun Jun 15 13:23:34 CEST 2003


Hi,
Maybe I'm not understanding you correctly, but wouldn't the following 
be the fastest way to compute the sum of a column:

 > x <- matrix(c(1:4462), nrow=4462, ncol=1) #setting up a column of 
4462 elements to be able to make a comparison
 > sum(x[,1])
[1] 9956953
 > system.time(sum(x[,1]))
[1] 0 0 0 0 0

As you can see this took so little time it actually came out as 0. And 
my system is not fast at all (powermac 9600 with a G4 700 MHz upgrade).
HTH, Jonck


> From: Jean Eid <jeaneid at chass.utoronto.ca>
> Date: zon jun 15, 2003  02:51:29 Europe/Amsterdam
> To: r-help at stat.math.ethz.ch
> Subject: [R] more efficient sum of matrix columns
>
>
> Dear R users,
> I am looking for a more efficient way to compute the sum of columns of 
> a
> matrix.
> I am currently using apply(data, 2, sum) however, I am building a data 
> set
> from another one by summing the columns of some parts of the matrix.
> the loop is taking too long (about 1/2 hour) for a 4462 * 202 matrix.
> thanks,
> Jean Eid




More information about the R-help mailing list