[R] Using dcast with multiple functions to aggregate
Ista Zahn
istazahn at gmail.com
Thu Apr 12 20:16:10 CEST 2012
Hi Jokel,
On Thu, Apr 12, 2012 at 7:57 AM, Jokel Meyer <jokel.meyer at googlemail.com> wrote:
> Dear R communitiy,
>
> I am trying to use multiple functions for aggregation within a function
> call for dcast. However this seems to result in an error. Also I have not
> managed to make dcast() work with fun.aggregate=sd. Please find attached
> some example code using the ChickWeight data.
>
> Many thanks for your help!
> Jokel
>
>
> #Chick weight example
>
> names(ChickWeight) <- tolower(names(ChickWeight))
>
>
> sd(ChickWeight$weight) # works fine
>
> mean(ChickWeight$weight) # works fine
>
> length(ChickWeight$weight) # works fine
>
>
> chick_m <- melt(ChickWeight, id=2:4, na.rm=TRUE)
This doesn't do anything interesting, because there is only one measure.var!
>
> dcast(chick_m, time~variable, mean) # works fine
>
> dcast(chick_m, time~variable, length) # works fine
>
>
> dcast(chick_m, time~variable, fun.aggregate=sd) # gives an error
Works on my machine. See session info below.
>
> dcast(chick_m, time~variable, c(mean, length)) # gives an error
?dcast tells you that fun.aggregate must return a single summary
statistic. dcast is not the right function for the job.
For reference, here is my session info:
R version 2.14.2 (2012-02-29)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] C
attached base packages:
[1] splines stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] plyr_1.7.1 reshape2_1.2.1 foreign_0.8-49 eha_2.0-7
[5] survival_2.36-12
loaded via a namespace (and not attached):
[1] compiler_2.14.2 stringr_0.6 tcltk_2.14.2 tools_2.14.2
Best,
Ista
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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