[R] flexible processing

Gabor Grothendieck ggrothendieck at gmail.com
Tue Oct 30 17:09:22 CET 2007


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



More information about the R-help mailing list