Re: [R] optimal way to compute matrix subtotals?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 12 May 2006 - 20:14:20 EST

On Fri, 12 May 2006, Werner Wernersen wrote:

> Hi!
>
> I have large matrices, one column per variable and I have a vector of
> factors / grouping symbols. Then I am computing subtotals for the groups
> but it takes pretty long and thus I wanted to ask if there is a better
> way to do it or if this is already the best way: subTotals <-
> function(x, groupvec)
> do.call("rbind",lapply(split(x,groupvec),colSums,na.rm=T))

I suggest you look at rowsum (on a transpose of your matrix). It will not handle NAs, though, if you have any.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
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 Fri May 12 20:17:42 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 12 May 2006 - 22:10:08 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.