Bertolt Meyer
2008-Aug-01 13:12 UTC
[R] Newbie question: How to use tapply() on several vectors simultaneously
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 -- Bertolt Meyer Oberassistent Sozialpsychologie, Psychologisches Institut der Universit?t Z?rich Binzm?hlestr. 14, Box 15 CH-8050 Z?rich bmeyer at sozpsy.uzh.ch tel: +41446357282 fax: +41446357279 mob: +41788966111
David Hajage
2008-Aug-01 13:20 UTC
[R] Newbie question: How to use tapply() on several vectors simultaneously
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20080801/ee9d83ea/attachment.pl>
Stephan Kolassa
2008-Aug-01 13:30 UTC
[R] Newbie question: How to use tapply() on several vectors simultaneously
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 >
Dimitris Rizopoulos
2008-Aug-01 13:30 UTC
[R] Newbie question: How to use tapply() on several vectors simultaneously
one option is aggregate(), e.g., test <- as.data.frame(cbind(c(rep(1,5),rep(2,5)), rnorm(10), rnorm(10))) names(test)[1] <- "groupID" aggregate(test[c("V2", "V3")], list(test$groupID), mean) I hope it helps. Best, Dimitris -- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://perswww.kuleuven.be/dimitris_rizopoulos/ Quoting Bertolt Meyer <bmeyer at sozpsy.uzh.ch>:> 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 > > -- > Bertolt Meyer > Oberassistent > Sozialpsychologie, Psychologisches Institut der Universit?t Z?rich > Binzm?hlestr. 14, Box 15 > CH-8050 Z?rich > > bmeyer at sozpsy.uzh.ch > tel: +41446357282 > fax: +41446357279 > mob: +41788966111 > > ______________________________________________ > R-help at 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.Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Apparently Analagous Threads
- Help with shading a polygon below a segment of a curve (normal distribution)
- vector indexing problem in multilevel data: assigning a specific value to all group members
- how to read the "Sum Sq" - column from summary.aov()
- marginality principle / selecting the right type of SS for an interaction hypothesis
- lmer() causes segfault