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 <[EMAIL PROTECTED]>:

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

[EMAIL PROTECTED]
tel:   +41446357282
fax:   +41446357279
mob:   +41788966111

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



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
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