[R] optimal way to compute matrix subtotals?

Petr Pikal petr.pikal at precheza.cz
Fri May 12 12:18:25 CEST 2006


not sure about speed but aggregate can probably do it.

aggregate(x, list(groupvec), sum, na.rm=T)


On 12 May 2006 at 11:48, Werner Wernersen wrote:

Date sent:      	Fri, 12 May 2006 11:48:45 +0200 (CEST)
From:           	Werner Wernersen <pensterfuzzer at yahoo.de>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] optimal way to compute matrix subtotals?

> 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))
> Thanks reading my question and any hints!
>  Werner
> ---------------------------------
>  [[alternative HTML version deleted]]
> ______________________________________________
> R-help at 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

Petr Pikal
petr.pikal at precheza.cz

More information about the R-help mailing list