r - Aggregate data frame by date and apply different functions to corresponding columns? -
i have following data frame "df" part of larger one:
x1 x2 x3 x4 x5 4468 2010-03-24 3 1.000000e+00 1 2 7662 2010-03-24 9 3.000000e+00 2 1 1272 2010-03-25 8 2.000000e+00 1 1 1273 2010-03-26 9 0.000000e+00 1 1 1274 2010-03-27 8 0.000000e+00 1 1 4469 2010-03-28 4 0.000000e+00 1 2 7663 2010-03-28 4 3.000000e+00 3 1 8734 2010-03-28 7 4.000000e+00 2 3 1275 2010-03-29 8 0.000000e+00 1 1
as can see first column contains date. want follows: want transform dataframe new 1 "df2" there 1 row per date corresponding column values:
x2, average x3, sum x4, maximum
of previous values per date. x5 not relevant , can removed. result:
x1 x2 x3 x4 7662 2010-03-24 6 4.000000e+00 2 1272 2010-03-25 8 2.000000e+00 1 1273 2010-03-26 9 0.000000e+00 1 1274 2010-03-27 8 0.000000e+00 1 8734 2010-03-28 5 7.000000e+00 3 1275 2010-03-29 8 0.000000e+00 1
does know how accomplish this? appreciated!
you can use ddply
function plyr package arbitrary aggregations or other transforms grouping variable.
for question code like:
library(plyr) result <- ddply(df, .(x1), function(df) { with(df, data.frame( x1=mean(x1), x2=sum(x2), x3=max(x3) ) ) } )
if medium-large project may want set progress
argument show progress bar. large problem can set use parallel processing.
Comments
Post a Comment