Hi Bertolt,

by(test,INDICES=test$groupID,FUN=mean)

And today's a holiday in Switzerland, so stop working already ;-)

HTH
Stephan



Bertolt Meyer schrieb:
Dear R users,

I have a newbie-question that I couldn't resolve after reading through several pieces of documentation and searching the archive.

I have a data.frame containing experimental data from a group experiment in psychology. Each line represents a single participant, but participants were assigned to groups of three or four persons. One variable indicates each participants' group number (groupID). For a large number of variables, I would like to obtain the mean group value. I figured I use tapply() in the fashion of tapply(variable, groupID, mean), but that would be a tiresome task for my 150 variables. I am thus looking for a way to obtain a data.frame that contains one row for each group with the group-mean variables as columns.

Example:

 > test <- as.data.frame(cbind(c(rep(1,5),rep(2,5)), rnorm(10), rnorm(10)))
 > names(test)[1] <- "groupID"
 > test

   groupID          V2          V3
1        1 -0.82990860 -0.61778919
2        1 -0.01379452  0.64609053
3        1 -2.64990839 -1.00570627
4        1 -0.07903878 -0.70864441
5        1  0.61483071 -1.32039565
6        2 -0.18913937  1.38490710
7        2 -0.60017953  0.15893421
8        2 -0.99901931  0.05963436
9        2 -1.46759515  0.35040283
10       2 -0.44650422 -0.08713162

 > tapply(test$V2, test$groupID, mean)
         1          2
-0.5915639 -0.7404875

 > tapply(test$V3, test$groupID, mean)
         1          2
-0.6012890  0.3733494

I am now looking for something that gives me

  groupID      V2        V3
1    1 -0.5915639    -0.6012890
2    2 -0.7404875     0.3733494

Any ideas?

Thank you very much,
Bertolt


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

Reply via email to