[R] creating conditional means

Sherri Heck sheck at ucar.edu
Sat Dec 1 23:21:33 CET 2007


Hi Gabor,

Thank you for your help.  I think I need to clarify a bit more.  I am 
trying to say

average all 2pms for months march + april + may (for example). I hope this is clearer.  

here's a larger subset of my data set:

year, month, hour, co2(ppm), num1,num2

2006 1 0 384.2055 14 14
2006 1 1 384.0304 14 14
2006 1 2 383.9672 14 14
2006 1 3 383.8452 14 14
2006 1 4 383.8594 14 14
2006 1 5 383.7318 14 14
2006 1 6 383.6439 14 14
2006 1 7 383.7019 14 14
2006 1 8 383.7487 14 14
2006 1 9 383.8376 14 14
2006 1 10 383.8684 14 14
2006 1 11 383.8301 14 14
2006 1 12 383.8058 14 14
2006 1 13 383.9419 14 14
2006 1 14 383.7876 14 14
2006 1 15 383.7744 14 14
2006 1 16 383.8566 14 14
2006 1 17 384.1014 14 14
2006 1 18 384.1312 14 14
2006 1 19 384.1551 14 14
2006 1 20 384.099 14 14
2006 1 21 384.1408 14 14
2006 1 22 384.3637 14 14
2006 1 23 384.1491 14 14
2006 2 0 384.7082 27 27
2006 2 1 384.6139 27 27
2006 2 2 384.7453 26 26
2006 2 3 384.9224 28 28
2006 2 4 384.8581 28 28
2006 2 5 384.9208 28 28
2006 2 6 384.9086 28 28
2006 2 7 384.837 28 28
2006 2 8 384.6163 27 27
2006 2 9 384.7406 28 28
2006 2 10 384.7468 28 28
2006 2 11 384.6992 28 28
2006 2 12 384.6388 28 28
2006 2 13 384.6346 28 28
2006 2 14 384.6037 28 28
2006 2 15 384.5295 28 28
2006 2 16 384.5654 28 28
2006 2 17 384.6466 28 28
2006 2 18 384.6344 28 28
2006 2 19 384.5911 28 28
2006 2 20 384.6084 28 28
2006 2 21 384.6318 28 28
2006 2 22 384.6181 27 27
2006 2 23 384.6087 27 27


thanks you again for your assistance-

s.heck


Gabor Grothendieck wrote:
> 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