I have the following data.frame: index <- c("a","a","b","b","b") alpha <- c(1,2,3,4,5) beta <- c(2,3,4,5,6) table <-data.frame(index,alpha,beta) I'm now interested in getting means of alpha and beta for each of the index values and do a tapply() for each of the columns, e.g. means.alpha <- tapply(table$alpha, index,mean) means.beta <- tapply(table$beta,index,mean) but as one tapply function, something like tapply(table[2:3], index, mean), but this clearly doesnt' work. Suggestions? Thanks, Andrew
Gabor Grothendieck
2007-Jun-18 03:49 UTC
[R] getting tapply() to work across multiple columns
Try aggregate: aggregate(table[2:3], table[1], mean) On 6/17/07, Andrew Yee <yee at post.harvard.edu> wrote:> I have the following data.frame: > > index <- c("a","a","b","b","b") > alpha <- c(1,2,3,4,5) > beta <- c(2,3,4,5,6) > table <-data.frame(index,alpha,beta) > > I'm now interested in getting means of alpha and beta for each of the > index values and do a tapply() for each of the columns, e.g. > > means.alpha <- tapply(table$alpha, index,mean) > means.beta <- tapply(table$beta,index,mean) > > but as one tapply function, something like > > tapply(table[2:3], index, mean), but this clearly doesnt' work. > > Suggestions? > > Thanks, > Andrew > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >