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! -- Mich?l Schnitz michel.schnitz at web.de
try this. The first group of lines recreates your data frame, DF, and the last line is the aggregate: Input <- "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 " DF <- read.table(textConnection(Input), header = TRUE, as.is = TRUE) DF$z <- as.numeric(sub(",", ".", DF$z)) DF$Date <- as.Date(DF$Date) aggregate(DF["z"], list(yearmon = format(DF$Date, "%Y-%m")), mean) On 4/23/07, Michel Schnitz <michel.schnitz at web.de> 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! > -- > Mich?l Schnitz > michel.schnitz at web.de > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
it works. thanks a lot. Gabor Grothendieck wrote:> try this. The first group of lines recreates your data frame, DF, and > the last line is the aggregate: > > > Input <- "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 > " > DF <- read.table(textConnection(Input), header = TRUE, as.is = TRUE) > DF$z <- as.numeric(sub(",", ".", DF$z)) > DF$Date <- as.Date(DF$Date) > > aggregate(DF["z"], list(yearmon = format(DF$Date, "%Y-%m")), mean) > > > > On 4/23/07, Michel Schnitz <michel.schnitz at web.de> 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! >> -- >> Mich?l Schnitz >> michel.schnitz at web.de >> >> ______________________________________________ >> R-help at stat.math.ethz.ch 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. >> >-- Mich?l Schnitz michel.schnitz at web.de Scharrenstrasse 07 06108 Halle-Saale phone: +0049-(0)345- 290 85 24 mobile:+0049-(0)176- 239 000 64
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! >