[R] A faster way to aggregate?

Dieter Menne dieter.menne at menne-biomed.de
Mon Jul 4 17:43:11 CEST 2005


Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:

> Note that you did not actually try my suggestion which was rowsum,
> not colMeans.

Mea culpa, I was hooked by rowSums, and so I was did not get aware of the 
grouping facility of rowsum().

 
> The following solution based on rowsum is more than
> an order of magnitude faster than any of the solutions in your
> posts:
> 
> 	sc1 <- as.matrix(sc[,-1])
> 	is.na.sc1 <- is.na(sc1)
> 	x1 <- rowsum(ifelse(is.na.sc1, 0, sc1), group)
> 	xx <- rowsum(1-is.na.sc1, group)
> 	res <- floor(100*x1/xx)

Thanks a lot.

Dieter




More information about the R-help mailing list