Dear List, I have a data frame containing reaction times of participants in some experiment. As usual each line is single trial in the experiment. Two factors denote the conditions in the experiment. Each participant completes different trials for each condition. Now, the question: I want to calculate per participant, per condition the mean reaction time and its standard deviation. I can do this using AGGREGATE(). However, I want to merge this info with the original data frame. This is, I want each line to contain the mean and SD of the reaction time for the participant and condition on that line. I have tried to solve this by looping trough data frame. For each line, I select using SUBSET() the lines that belong to the same participant and condition. Then I calculate the average/SD. But this takes a long time. BYTW: I find that finding proper subject for r-help list mails, is very hard. So, if any one knows a set of better keywords... Any ideas? Thanks, Dieter Vanderelst ------------------------------------------ Dieter Vanderelst dieter _ vanderelst AT emailengine DOT org d DOT vanderelst AT tue DOT nl Eindhoven University of Technology Faculty of Industrial Design Designed Intelligence Group Den Dolech 2 5612 AZ Eindhoven The Netherlands Tel +31 40 247 91 11
I have been trying to figure out how Hadley Wickhams "reshape" package works and I tried it on what may be your problem. Here is my example. Does it do something like what you want? ==============================================# Test Data zz <- "subj cond t1 t2 t3 A 1 4 5 7 A 2 5 2 8 B 2 3 7 3 C 1 5 4 4 B 1 4 5 5 C 2 3 7 7" mydata <- read.table(textConnection(zz), header=TRUE, as.is=TRUE); mydata library(reshape) # Melt the data.frame df1 <- melt(mydata, id=c("subj", "cond"), measured=c("t1","t2","t3")) ; df1 # calculate the mean and sd stats <- cast(df1, subj + cond ~ . , function(x) c(mean= mean(x), sd=sd(x))) # Merge the original data.frame and the stats data.frame to get the # desired results. combined.results <- merge(mydata,stats, by=c("subj","cond")); combined.results ================================================--- Dieter Vanderelst <dieter_vanderelst at emailengine.org> wrote:> Dear List, > > I have a data frame containing reaction times of > participants in some experiment. > > As usual each line is single trial in the > experiment. Two factors denote the conditions in the > experiment. Each participant completes different > trials for each condition. > > Now, the question: > > I want to calculate per participant, per condition > the mean reaction time and its standard deviation. > > I can do this using AGGREGATE(). However, I want to > merge this info with the original data frame. This > is, I want each line to contain the mean and SD of > the reaction time for the participant and condition > on that line. > > I have tried to solve this by looping trough data > frame. For each line, I select using SUBSET() the > lines that belong to the same participant and > condition. Then I calculate the average/SD. But this > takes a long time. > > BYTW: I find that finding proper subject for r-help > list mails, is very hard. So, if any one knows a set > of better keywords...> > Any ideas?Perhaps a random word generator?> > Thanks, > Dieter Vanderelst
Does this do what you want?> x <- data.frame(subj=sample(1:3,20,TRUE), cond=sample(1:2,20,TRUE), time=runif(20,1,10)) > # add mean and sd > x$mean <- ave(x$time, x$subj, x$cond) > x$sd <- ave(x$time, x$subj, x$cond, FUN=sd) > xsubj cond time mean sd 1 3 2 3.563114 5.637710 2.879756 2 2 2 9.055847 6.641738 2.051851 3 2 1 5.016118 5.955314 2.777777 4 1 1 8.019864 8.079528 0.598062 5 3 2 8.925571 5.637710 2.879756 6 2 1 4.718118 5.955314 2.777777 7 2 1 1.574276 5.955314 2.777777 8 1 2 4.019387 6.292459 3.214609 9 1 1 7.513534 8.079528 0.598062 10 2 2 4.038538 6.641738 2.051851 11 2 2 6.673727 6.641738 2.051851 12 1 2 8.565531 6.292459 3.214609 13 1 1 8.705185 8.079528 0.598062 14 2 1 4.522234 5.955314 2.777777 15 3 2 4.424445 5.637710 2.879756 16 2 1 9.059009 5.955314 2.777777 17 2 2 6.798842 6.641738 2.051851 18 2 1 7.669708 5.955314 2.777777 19 3 1 6.447731 6.447731 NA 20 2 1 9.127735 5.955314 2.777777 On 10/19/07, Dieter Vanderelst <dieter_vanderelst at emailengine.org> wrote:> Dear List, > > I have a data frame containing reaction times of participants in some experiment. > > As usual each line is single trial in the experiment. Two factors denote the conditions in the experiment. Each participant completes different trials for each condition. > > Now, the question: > > I want to calculate per participant, per condition the mean reaction time and its standard deviation. > > I can do this using AGGREGATE(). However, I want to merge this info with the original data frame. This is, I want each line to contain the mean and SD of the reaction time for the participant and condition on that line. > > I have tried to solve this by looping trough data frame. For each line, I select using SUBSET() the lines that belong to the same participant and condition. Then I calculate the average/SD. But this takes a long time. > > BYTW: I find that finding proper subject for r-help list mails, is very hard. So, if any one knows a set of better keywords... > > Any ideas? > > Thanks, > Dieter Vanderelst > > ------------------------------------------ > Dieter Vanderelst > > dieter _ vanderelst AT emailengine DOT org > d DOT vanderelst AT tue DOT nl > > Eindhoven University of Technology > Faculty of Industrial Design > Designed Intelligence Group > Den Dolech 2 > 5612 AZ Eindhoven > The Netherlands > Tel +31 40 247 91 11 > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?