[R] getting tapply() to work across multiple columns

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 18 05:49:44 CEST 2007


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.
>



More information about the R-help mailing list