[R] Odp: reshape/aggregate

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 31 13:13:01 CEST 2011


Hi

> Hi all,
> I apologize for this probably stupid question, but I really can't figure 
it
> out.
> I have a dataframe like this:
> 
> group <- c(rep('A', 8), rep('B', 15), rep('C', 6))
> time <- c(rep(seq(1:4), 2), rep(seq(1:5), 3), rep(seq(1:3), 2))
> value <- runif (29, 1, 10)
> dfx <- data.frame (group, time, value)
> 
> I want to calculate mean and standard deviation for all values that 
belong
> to the same group and the same time and end up with a dataframe with the
> columns time, group, mean and sd that contains the calculated values for
> every group at every time point only once (12).
> What is the most elegant way to do this? Oh, and I would like to avoid
> renaming columns (like the _X1/_X2 created by casting with multiple
> functions), if possible.
> I am sure that this is pretty basic, but I have already wasted a 
ridiculous
> amount of time on this.

see
?aggregate

aggregate(dfx$value, list(group=dfx$group, time=dfx$time), function(x) 
c(mean(x), sd(x)))
 
and maybe also plyr package could help you

Regards
Petr


> 
> Thanks,
> 
> Kai
> 
>    [[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