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

Andrew Yee yee at post.harvard.edu
Mon Jun 18 05:38:29 CEST 2007


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



More information about the R-help mailing list