Hello, unfortunately, I don't know a better subject. I would like to be very flexible in how to process my data. Assume the following dataset: par1 <- seq(0,1,length.out = 100) par2 <- seq(1,100) fac1 <- factor(rep(c("group1", "group2"), each = 50)) fac2 <- factor(rep(c("group3", "group4", "group5", "group6"), each = 25)) df <- data.frame(par1, par2, fac1, fac2) Now, I would like to calculate e.g. the "sum" for par1 grouping by "fac1" and the "mean" and "sd" for par2 grouping by "fac1". I would like to determine this method as a string. In the end, I would like to have something like this: fac1.analysis: par1.sum par2.mean par2.sd group1 xxx xxx xxx group2 xxx xxx xxx fac2.analysis: ... Does anybody have any idea how to realize it? Antje
Hi, No is the more elegant way analysis <- lapply(c("fac1", "fac2"), function(x){ cbind.data.frame(par1.sum= do.call("rbind",lapply(lapply(split(df, eval(parse(text=x))), "[[", 1), sum)), par2.mean=do.call("rbind",lapply(lapply(split(df, eval(parse(text=x))), "[[", 2), mean)), par2.sd= do.call("rbind",lapply(lapply(split(df, eval(parse(text=x))), "[[", 2), mean))) }) On 30/10/2007, Antje <niederlein-rstat@yahoo.de> wrote:> > Hello, > > unfortunately, I don't know a better subject. I would like to be very > flexible > in how to process my data. > Assume the following dataset: > > par1 <- seq(0,1,length.out = 100) > par2 <- seq(1,100) > fac1 <- factor(rep(c("group1", "group2"), each = 50)) > fac2 <- factor(rep(c("group3", "group4", "group5", "group6"), each = 25)) > > df <- data.frame(par1, par2, fac1, fac2) > > Now, I would like to calculate e.g. the "sum" for par1 grouping by "fac1" > and > the "mean" and "sd" for par2 grouping by "fac1". > > I would like to determine this method as a string. > > In the end, I would like to have something like this: > > fac1.analysis: > > par1.sum par2.mean par2.sd > group1 xxx xxx xxx > group2 xxx xxx xxx > > fac2.analysis: > > ... > > Does anybody have any idea how to realize it? > > Antje > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Check out summaryBy in the doBy package:> library(doBy) > summaryBy(. ~ fac1, data = df, FUN = c(sum, mean, sd))[c(1,2,5,7)]fac1 par1.sum par2.mean par2.sd 1 group1 12.37374 25.5 14.57738 2 group2 37.62626 75.5 14.57738> summaryBy(. ~ fac2, data = df, FUN = c(sum, mean, sd))[c(1,2,5,7)]fac2 par1.sum par2.mean par2.sd 1 group3 3.030303 13 7.3598 2 group4 9.343434 38 7.3598 3 group5 15.656566 63 7.3598 4 group6 21.969697 88 7.3598 If you don't mind a few extra columns you can omit the [c(...)] part. On Oct 30, 2007 10:18 AM, Antje <niederlein-rstat at yahoo.de> wrote:> Hello, > > unfortunately, I don't know a better subject. I would like to be very flexible > in how to process my data. > Assume the following dataset: > > par1 <- seq(0,1,length.out = 100) > par2 <- seq(1,100) > fac1 <- factor(rep(c("group1", "group2"), each = 50)) > fac2 <- factor(rep(c("group3", "group4", "group5", "group6"), each = 25)) > > df <- data.frame(par1, par2, fac1, fac2) > > Now, I would like to calculate e.g. the "sum" for par1 grouping by "fac1" and > the "mean" and "sd" for par2 grouping by "fac1". > > I would like to determine this method as a string. > > In the end, I would like to have something like this: > > fac1.analysis: > > par1.sum par2.mean par2.sd > group1 xxx xxx xxx > group2 xxx xxx xxx > > fac2.analysis: > > ... > > Does anybody have any idea how to realize it? > > Antje > > ______________________________________________ > 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. >