Re: [R] Computing sums of the columns of an array

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Sat 06 Aug 2005 - 02:39:03 EST

On Fri, 2005-08-05 at 12:16 -0400, Martin C. Martin wrote:
> Hi,
>
> I have a 5x731 array A, and I want to compute the sums of the columns.
> Currently I do:
>
> apply(A, 2, sum)
>
> But it turns out, this is slow: 70% of my CPU time is spent here, even
> though there are many complicated steps in my computation.
>
> Is there a faster way?

Yes, colSums()

e.g.:

> set.seed(1234)
> dat <- matrix(runif(5*731), ncol = 731)
> system.time(for(i in 1:1000) apply(dat, 2, sum), gcFirst = TRUE)
[1] 8.05 0.00 9.89 0.00 0.00
> system.time(for(i in 1:1000) colSums(dat), gcFirst = TRUE)
[1] 0.09 0.01 0.09 0.00 0.00

But neither is that slow on my system. What is A?

HTH Gav

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
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
Received on Sat Aug 06 02:46:10 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:42 EST