[R] creating conditional means

Gabor Grothendieck ggrothendieck at gmail.com
Sat Dec 1 22:44:46 CET 2007


Try aggregate:


Lines <- "Year Month Hour co2 num1 num2
 2006   11    0 383.3709   28   28
 2006   11    1 383.3709   28   28
 2006   11    2 383.3709   28   28
 2006   11    3 383.3709   28   28
 2006   11    4 383.3709   28   28
 2006   11    5 383.3709   28   28
 2006   11    6 383.3709   28   28
 2006   11    7 383.3709   28   28
 2006   11    8 383.3709   28   28
 2006   11    9 383.3709   27   27
 2006   11   10 383.3709   28   28
"
DF <- read.table(textConnection(Lines), header = TRUE)
aggregate(DF[4:6],
   with(DF, data.frame(Year, Qtr = (Month - 1) %/% 3 + 1, Hour)),
   mean)

On Dec 1, 2007 3:57 PM, Sherri Heck <sheck at ucar.edu> wrote:
> Hi all-
>
> I have a dataset (year, month, hour, co2(ppm), num1,num2)
>
>
> [49,] 2006   11    0 383.3709   28   28
> [50,] 2006   11    1 383.3709   28   28
> [51,] 2006   11    2 383.3709   28   28
> [52,] 2006   11    3 383.3709   28   28
> [53,] 2006   11    4 383.3709   28   28
> [54,] 2006   11    5 383.3709   28   28
> [55,] 2006   11    6 383.3709   28   28
> [56,] 2006   11    7 383.3709   28   28
> [57,] 2006   11    8 383.3709   28   28
> [58,] 2006   11    9 383.3709   27   27
> [59,] 2006   11   10 383.3709   28   28
>
> that repeats in this style for each month.  I would like to compute the
> mean for each hour in three month intervals.
> i.e.  average all 2pms for each day for months march, april and may. and
> then do this for each hour interval.
> i have been messing around with 'for loops' but can't seem to get the
> output I want.
>
> thanks in advance for any help-
>
> s.heck
> CU, Boulder
>
> ______________________________________________
> 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