>> aggregate(m[,-c(1:2)], by=list(m[,1]), mysum) <----------------- this >> computes correctly. > Group.1 C D > 1 A 3 2 > 2 B 15 13 > 3 C 10 10 > 4 D 6 7 > 5 E 9 8 > >> aggregate(m[,-c(1:2)], by=list(m[,1]), mylength) <----------------- this >> computes correctly. > Group.1 C D > 1 A 1 1 > 2 B 5 4 > 3 C 3 4 > 4 D 2 3 > 5 E 4 4 > > There are other statistics I need to compute e.g. var, sd, and it is a hassle > to create customized versions to exclude NA. Any alternative approaches ?
How about writing a function to do the customisation for you? na.rm <- function(f) { function(x, ...) f(x[!is.na(x)], ...) } aggregate(m[,-c(1:2)], by=list(m[,1]), na.rm(sum)) aggregate(m[,-c(1:2)], by=list(m[,1]), na.rm(length)) Hadley -- http://had.co.nz/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.