Skip to content
Prev 259266 / 398502 Next

how to calculate the mean of a group in a table

I see you already have three solutions but, just for the heck of it, here's another. I am trying to get familiar with the reshape2 package and your question was a good exercise for me. 


With your data set named xx:

library(reshape2)

yy <- melt(xx, id=c("period", "treatment", "session",
     "type","stage"), measured=c("wage_accepted"))

dcast(yy, period + stage ~ variable, mean)

As for your request for tutorials for loops and if-else materials, I cannot make any better suggestions than you already have. However, in R, if you are thinking of using these it usually means you are not thinking properly in R.  There are "usually" faster and better was of achieving the desired results in R. Sometimes a loop or if-else is appropriate but usually in many fewer instances than in most other languages.

By the way type is a reserved(? name in R. Not a problem here but it's safer not to use reserved words in R.
--- On Sat, 5/7/11, tornanddesperate <MatthiasNeff at gmx.ch> wrote: