[R] aggregate function
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Apr 23 14:19:45 CEST 2007
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.
>
More information about the R-help
mailing list