# [R] Calculating Mean

Mon Jun 3 15:33:46 CEST 2013

```Hello,

Try

aggregate(latency ~ state + conditionNo + subject, data = dat, FUN = mean)

Em 03-06-2013 12:29, Laura Thomas escreveu:
> Hi,
>
> Thanks all for the help. I have used the dput function which I have posted below. What I am trying to do is calculate the mean latency for each state(1-8), in each condition (1-10), for each participant (n=13). I know for this to be most efficient I would need some form of loop, but this is a bit about my R abilities at the moment, I am quite happy to run the code a few times to get the information that I would need.
>
> Thanks a lot for any help,
>
> Laura
>
> structure(list(subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L), conditionNo = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1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 2L, 8L, 6L, 8L,
> 5L, 8L, 3L, 2L, 4L, 4L, 6L, 3L, 7L, 4L, 4L, 8L, 3L, 5L, 5L, 2L,
> 4L, 2L, 1L, 7L, 2L, 5L, 1L, 5L, 1L, 3L, 4L, 3L, 6L, 5L, 6L, 7L,
> 2L, 3L, 8L, 4L, 6L, 1L, 4L, 3L, 6L, 1L, 8L, 1L, 3L, 2L, 4L, 7L,
> 5L, 6L, 2L, 7L, 3L, 6L, 4L, 3L, 2L, 5L, 6L, 4L, 5L, 2L, 6L, 6L,
> 7L, 3L, 4L, 3L, 1L, 8L, 5L, 5L, 5L, 4L, 2L, 1L, 5L, 2L), latency = c(869L,
> 864L, 1004L, 801L, 611L, 679L, 649L, 603L, 677L, 614L, 619L,
> 542L, 613L, 746L, 713L, 714L, 586L, 542L, 613L, 585L, 799L, 807L,
> 868L, 670L, 672L, 706L, 739L, 546L, 681L, 683L, 604L, 608L, 773L,
> 643L, 743L, 524L, 579L, 617L, 604L, 609L, 967L, 540L, 514L, 651L,
> 508L, 548L, 506L, 578L, 763L, 585L, 1088L, 609L, 549L, 603L,
> 582L, 764L, 618L, 606L, 918L, 959L, 775L, 676L, 678L, 584L, 611L,
> 583L, 614L, 747L, 618L, 702L, 800L, 709L, 742L, 645L, 648L, 747L,
> 650L, 669L, 703L, 704L, 544L, 550L, 508L, 869L, 680L, 843L, 839L,
> 578L, 776L, 678L, 519L, 606L, 578L, 648L, 602L, 865L, 803L, 639L,
> 610L, 614L, 747L, 715L, 587L, 672L, 674L, 579L, 713L, 617L, 637L,
> 613L, 810L, 646L, 585L, 541L, 644L, 583L, 571L, 738L, 609L, 613L,
> 617L, 604L, 576L, 646L, 488L, 512L, 584L, 637L, 511L, 551L, 583L,
> 572L, 738L, 609L, 709L, 614L, 690L, 708L, 580L, 687L, 575L, 709L,
> 581L, 651L, 1009L, 646L, 649L, 764L, 843L, 697L, 746L, 649L,
> 870L, 511L, 583L, 604L, 629L, 893L, 518L, 1023L, 675L, 838L,
> 673L, 739L, 578L, 647L, 650L, 829L, 746L, 649L)), .Names = c("subject",
> "conditionNo", "state", "latency"), row.names = c(3L, 4L, 5L,
> 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
> 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
> 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
> 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
> 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L,
> 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L,
> 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L,
> 97L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L,
> 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L,
> 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L,
> 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L,
> 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L,
> 153L, 154L, 155L, 157L, 158L, 159L, 160L, 162L, 164L, 165L, 166L,
> 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L), class = "data.frame")
>
> On 3 Jun 2013, at 11:20, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>> Hello,
>>
>> Like Ivan said, you should give us a data example, the best way is to do it is to paste the output of ?dput in a post. If your data frame is named 'dat' use the following.
>>
>> dput(head(dat, 50))  # paste the output of this in a post
>>
>> As for the question, here is an example using ?aggregate, with fake data.
>>
>>
>> dat <- data.frame(X = rnorm(100), A = sample(8, 100, TRUE))
>>
>> aggregate(X ~ A, data = dat, FUN = mean)
>>
>>
>> Hope this helps,
>>
>>
>> Em 03-06-2013 10:15, Laura Thomas escreveu:
>>> Hi All,
>>>
>>>
>>> I have a data file which has a dependent variable (reaction time) and a couple of independent variables, one of which is coded 1-8; I want to calculate the reaction time for each of the 8 codes of the independent variable.
>>>
>>> Thanks for any help,
>>>
>>> Laura
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help