Hi I've been reading the help for by and aggregate but can't get my head round how to do this. I have a data frame - the first three columns are replicate measurements, then the next 3 are replicates etc up to 36 (so 12 variables with 3 replicate measurements each). I want to compute the mean for each of the 12 variables, so that, for each row, I have 12 means. A grouping variable across columns can easily be created by rep(1:12,each=3), but I can't figure out which function to use to get R to calculate the means I want. Thanks in advance Mick
On 3/6/2006 7:13 AM, michael watson (IAH-C) wrote:> Hi > > I've been reading the help for by and aggregate but can't get my head > round how to do this. > > I have a data frame - the first three columns are replicate > measurements, then the next 3 are replicates etc up to 36 (so 12 > variables with 3 replicate measurements each). I want to compute the > mean for each of the 12 variables, so that, for each row, I have 12 > means. > > A grouping variable across columns can easily be created by > rep(1:12,each=3), but I can't figure out which function to use to get R > to calculate the means I want.I'd think the easiest thing is to reshape the dataset so you have the variables entirely in their own columns, then just apply mean to the columns. (Add a new column for replicate number so you don't lose that information.) If you want to avoid that, then code like this will do your calculation, but it looks ugly: means <- numeric(12) for (i in 0:11) { means[i] <- mean(mydata[,3*i + 1:3]) } You could also put something together using tapply and your grouping variable, but I think the two solutions above will be easiest to read. Duncan Murdoch
t(as.matrix(aggregate(t(as.matrix(DF)), rep(1:12,each=3), mean)[,-1])) michael watson (IAH-C) a ?crit :>Hi > >I've been reading the help for by and aggregate but can't get my head >round how to do this. > >I have a data frame - the first three columns are replicate >measurements, then the next 3 are replicates etc up to 36 (so 12 >variables with 3 replicate measurements each). I want to compute the >mean for each of the 12 variables, so that, for each row, I have 12 >means. > >A grouping variable across columns can easily be created by >rep(1:12,each=3), but I can't figure out which function to use to get R >to calculate the means I want. > >Thanks in advance >Mick > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! R-project.org/posting-guide.html > > >
On 3/6/06 7:13 AM, "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk> wrote:> Hi > > I've been reading the help for by and aggregate but can't get my head > round how to do this. > > I have a data frame - the first three columns are replicate > measurements, then the next 3 are replicates etc up to 36 (so 12 > variables with 3 replicate measurements each). I want to compute the > mean for each of the 12 variables, so that, for each row, I have 12 > means. > > A grouping variable across columns can easily be created by > rep(1:12,each=3), but I can't figure out which function to use to get R > to calculate the means I want.Hi, Mick. How about this? a <- matrix(rnorm(360),nr=10) b <- rep(1:12,each=3) avgmat <- aggregate(a,by=list(b)) Sean
sorry, i forgot list(): t(as.matrix(aggregate(t(as.matrix(DF)), list(rep(1:12,each=3)), mean)[,-1])) michael watson (IAH-C) a ?crit :>Hi > >I've been reading the help for by and aggregate but can't get my head >round how to do this. > >I have a data frame - the first three columns are replicate >measurements, then the next 3 are replicates etc up to 36 (so 12 >variables with 3 replicate measurements each). I want to compute the >mean for each of the 12 variables, so that, for each row, I have 12 >means. > >A grouping variable across columns can easily be created by >rep(1:12,each=3), but I can't figure out which function to use to get R >to calculate the means I want. > >Thanks in advance >Mick > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! R-project.org/posting-guide.html > > >