Re: [R] Collapsing across trials
Try aggregate(DF[4], DF[1:3], mean) On 12/16/06, Pedro Alcocer <[EMAIL PROTECTED]> wrote: > Hello, > > My ultimate goal is a repeated measures (mixed model) ANOVA, however, > my present question is about how to reorganize my data into the format > that the ANOVA commands expect. In particular, how to collapse across > trials. (I am using the tutorial at > [http://personality-project.org/r/r.anova.html] for the mixed model > ANOVA) > > The data I am using looks like this. A subject sees 10 trials per > condition and there are 2 conditions. I want the average of all the > reaction times (RTs) from a subject looking a one condition. I also > want to retain List in the final output. > > SubjListCondition RT > 2 1 C 338 > 2 1 C 227 > 2 1 C 430 > 2 1 C 621 > 2 1 C 255 > 2 1 C 348 > 2 1 C 280 > 2 1 C 356 > 2 1 C 272 > 2 1 C 346 > 3 2 C 489 > 3 2 C 426 > 3 2 C 352 > 3 2 C 351 > 3 2 C 349 > 3 2 C 403 > 3 2 C 336 > 3 2 C 278 > 3 2 C 365 > 3 2 C 271 > 2 1 D 360 > 2 1 D 374 > 2 1 D 326 > 2 1 D 363 > 2 1 D 290 > 2 1 D 458 > 2 1 D 295 > 2 1 D 362 > 2 1 D 285 > 2 1 D 277 > 3 2 D 354 > 3 2 D 352 > 3 2 D 362 > 3 2 D 360 > 3 2 D 334 > 3 2 D 365 > 3 2 D 335 > 3 2 D 391 > 3 2 D 272 > 3 2 D 618 > > The result should look like this: > 2 1 C (AVG) > 3 2 C (AVG) > 2 1 D (AVG) > 3 2 D (AVG) > > Where (AVG) is the average of the 10 trials. > > The above is a simplified case. How can I do this with multiple RT > measurements per subject? In other words, the above, but with more > than one RT column per subject. > > Resulting in: > > SubjListCondition RT1 RT2 RT3 > RT4 RT5 > 2 1 C (AVG1) (AVG2) (AVG2) (AVG2) (AVG2) > 3 2 C (AVG1) (AVG2) (AVG2) (AVG2) (AVG2) > 2 1 D (AVG1) (AVG2) (AVG2) (AVG2) (AVG2) > 3 2 D (AVG1) (AVG2) (AVG2) (AVG2) (AVG2) > > I've come across the apply and aggregate functions in online > documentation, and I have the suspicion that they may be called for > here, but their application isn't clear to me. I am fairly new to R. > > (I am using R 2.4.0 on Mac OS X.) > > I'd appreciate any insights. > > Pedro Alcocer > University of Florida > > __ > R-help@stat.math.ethz.ch 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. > __ R-help@stat.math.ethz.ch 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.
Re: [R] Collapsing across trials
Thanks so much, Hadley. That's exactly what I needed. My only comment is that the line cast(Subj + List + Condition ~ variable , mean) should be cast(dfm, Subj + List + Condition ~ variable , mean) Your excellent demo on your site cleared that up quickly, however. Thanks again, Pedro On 12/17/06, hadley wickham <[EMAIL PROTECTED]> wrote: > On 12/16/06, Pedro Alcocer <[EMAIL PROTECTED]> wrote: > > Hello, > > > > My ultimate goal is a repeated measures (mixed model) ANOVA, however, > > my present question is about how to reorganize my data into the format > > that the ANOVA commands expect. In particular, how to collapse across > > trials. (I am using the tutorial at > > [http://personality-project.org/r/r.anova.html] for the mixed model > > ANOVA) > > > > The data I am using looks like this. A subject sees 10 trials per > > condition and there are 2 conditions. I want the average of all the > > reaction times (RTs) from a subject looking a one condition. I also > > want to retain List in the final output. > > Have a look at the reshape package, http://had.co.nz/reshape. > > The following code should do what you want: > > install.packages("reshape") > library(reshape) > dfm <- melt(df, id=c("Subj", "List","Condition")) > cast(Subj + List + Condition ~ variable , mean) > > (that should work with any number of rt variables) > > Hadley > __ R-help@stat.math.ethz.ch 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.
Re: [R] Collapsing across trials
On 12/16/06, Pedro Alcocer <[EMAIL PROTECTED]> wrote: > Hello, > > My ultimate goal is a repeated measures (mixed model) ANOVA, however, > my present question is about how to reorganize my data into the format > that the ANOVA commands expect. In particular, how to collapse across > trials. (I am using the tutorial at > [http://personality-project.org/r/r.anova.html] for the mixed model > ANOVA) > > The data I am using looks like this. A subject sees 10 trials per > condition and there are 2 conditions. I want the average of all the > reaction times (RTs) from a subject looking a one condition. I also > want to retain List in the final output. Have a look at the reshape package, http://had.co.nz/reshape. The following code should do what you want: install.packages("reshape") library(reshape) dfm <- melt(df, id=c("Subj", "List","Condition")) cast(Subj + List + Condition ~ variable , mean) (that should work with any number of rt variables) Hadley __ R-help@stat.math.ethz.ch 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.