[R] Calculating Mean

arun smartpink111 at yahoo.com
Mon Jun 3 13:45:33 CEST 2013


Hi,
May be this is what you wanted:
dat2: dataset
library(plyr)

res1<-ddply(dat2,.(subject,conditionNo,state),summarize,MLat=mean(latency))
res1  

#subject conditionNo state     MLat
#1        1           1     1 674.8947
#2        1           1     2 649.5000
#3        1           1     3 662.6316
#4        1           1     4 683.3500
#5        1           1     5 650.7000
#6        1           1     6 650.0000
#7        1           1     7 674.1579
#8        1           1     8 674.1500
#9        1           2     1 752.0000
#10       1           2     2 649.5000
#11       1           2     3 958.0000
#12       1           2     4 582.5000
#13       1           2     5 684.0000
#14       1           2     7 629.0000
#15       1           2     8 838.0000
#or 

res2<-aggregate(latency~.,data=dat2,mean)
 res2<-res2[order(res2[,1],res2[,2],res2[,3]),]
 colnames(res2)<-colnames(res1)
 row.names(res2)<-1:nrow(res2)
 identical(res1,res2)
#[1] TRUE
A.K.



----- Original Message -----
From: Laura Thomas <skagandbonegirl at hotmail.com>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help at r-project.org
Sent: Monday, June 3, 2013 7:29 AM
Subject: Re: [R] Calculating Mean

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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 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), state = c(5L, 8L, 7L, 8L, 1L, 7L, 8L, 8L, 8L, 7L, 1L, 1L, 
5L, 7L, 1L, 4L, 3L, 2L, 8L, 3L, 7L, 6L, 6L, 4L, 2L, 5L, 1L, 5L, 
2L, 2L, 8L, 7L, 5L, 7L, 3L, 1L, 7L, 5L, 7L, 8L, 8L, 6L, 7L, 5L, 
5L, 1L, 4L, 3L, 6L, 2L, 4L, 5L, 8L, 2L, 8L, 5L, 7L, 2L, 4L, 1L, 
3L, 8L, 6L, 4L, 3L, 6L, 2L, 3L, 5L, 6L, 7L, 4L, 4L, 8L, 4L, 8L, 
1L, 3L, 1L, 1L, 7L, 6L, 3L, 1L, 7L, 1L, 2L, 2L, 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,
> 
> Rui Barradas
> 
> Em 03-06-2013 10:15, Laura Thomas escreveu:
>> Hi All,
>> 
>> Sorry about this quite basic, but I am very new to R.
>> 
>> 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
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 

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




More information about the R-help mailing list