[R] more efficient sum of matrix columns

John Fox jfox at mcmaster.ca
Sun Jun 15 04:47:20 CEST 2003


Dear Jean and Jonathan,

colSums() should be more efficient, but (unless I misunderstand the size of 
the problem) a problem this small shouldn't take a half hour. On my ageing 
800 MHz, 512MB Windows 2000 PC, the result was essentially instantaneous 
either way, though an order of magnitude faster with colSums:

 > data <- matrix(rnorm(4462 * 202), 4462, 202)
 > dim(data)
[1] 4462  202
 > system.time(apply(data, 2, sum))
[1] 0.38 0.00 0.42   NA   NA
 > system.time(colSums(data))
[1] 0.03 0.00 0.03   NA   NA

Jean does make reference to summing parts of a matrix, apparently in a 
loop, so it might help to know what's being done besides the column sums.

Regards,
  John

At 09:18 PM 6/14/2003 -0400, Jonathan Baron wrote:
>On 06/14/03 20:51, Jean Eid wrote:
> >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.
>
>colSums() might be faster, but I don't know how much faster.  It
>does not allow na.rm=T, but you don't have that, so it might
>help.

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox




More information about the R-help mailing list