[R] Newbie question: How to use tapply() on several vectorssimultaneously
ONKELINX, Thierry
Thierry.ONKELINX at inbo.be
Fri Aug 1 15:32:56 CEST 2008
Another option is ?by
test <- as.data.frame(cbind(c(rep(1,5),rep(2,5)), rnorm(10), rnorm(10)))
names(test)[1] <- "groupID"
test$groupID <- factor(test$groupID)
by(test[, -1], test$groupID, mean)
HTH,
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens David Hajage
Verzonden: vrijdag 1 augustus 2008 15:21
Aan: Bertolt Meyer
CC: r-help op r-project.org
Onderwerp: Re: [R] Newbie question: How to use tapply() on several vectorssimultaneously
something like that should work :
aggregate(test, list(test[,1]), mean)
2008/8/1 Bertolt Meyer <bmeyer op 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 op sozpsy.uzh.ch
> tel: +41446357282
> fax: +41446357279
> mob: +41788966111
>
> ______________________________________________
> R-help op 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list