[R] aggregate function
Martin Becker
martin.becker at mx.uni-saarland.de
Mon Apr 23 18:33:00 CEST 2007
If "monthly" should aggregate per "yyyy-mm" combination, you could try
something like
aggregate(x$z,list(cut(as.Date(x$Date),"m")),mean)
for monthly aggregation and
aggregate(x$z,list(cut(as.Date(x$Date),"y")),mean)
for yearly means.
If monthly aggregation should aggregate over different years (and
produce only 12 numbers), maybe
aggregate(x$z, list(format(as.Date(x$Date),"%m")),mean)
works (everything untested).
Be sure to use R 2.4.1 patched or 2.5.0, since there was a bug in
cut.Date which prevents the yearly aggregation from working properly
before R 2.4.1 patched!
Regards,
Martin
Michel Schnitz wrote:
> Hello,
>
> is there a way to use the aggregate function to calculate monthly mean
> in case i have one row in data frame that holds the date like
> yyyy-mm-dd? i know that it works for daily means. i also like to do it
> for monthly and yearly means. maybe there is something like aggregate(x,
> list(Date[%m]), mean)?
> the data frame looks like:
>
> Date Time z
> 2006-01-01 21:00 6,2
> 2006-01-01 22:00 5,7
> 2006-01-01 23:00 3,2
> 2006-01-02 00:00 7,8
> 2006-01-02 01:00 6,8
> 2006-01-02 02:00 5,6
> .
> .
> .
> 2007-03-30 22:00 5,2
> 2007-03-30 23:00 8,3
> 2007-03-31 00:00 6,4
> 2007-03-31 01:00 7,4
>
> thanks for help!
>
More information about the R-help
mailing list