[R] Calculate a mean for several months for several years

John Kane jrkrideau at yahoo.ca
Tue Feb 22 15:24:35 CET 2011



--- On Tue, 2/22/11, D Kelly O'Day <koday at processtrends.com> wrote:

> From: D Kelly O'Day <koday at processtrends.com>
> Subject: Re: [R] Calculate a mean for several months for several years
> To: r-help at r-project.org
> Received: Tuesday, February 22, 2011, 12:21 AM
> 
> Pete
> 
> The original question "I would like to calculate the
> average of the mean
> temperature for the summer months (Juli, August, September)
> for each of the
> 20 years"
> 
> Your codes gives mean for each of the 3 months, not for the
> 20 summers.
>  
> Here is a way to get the summer average for each of the 20
> years:
> 
>  set.seed = 1
>  d=data.frame(year=rep(1980:1999,each=12),
> month=rep(1:12,20), meanTemp =
> rnorm(240,10,5))
>  sum_d <- subset(d, d[,2]>=7 & temp_df[,2]
> <=9)
>  aggregate(meanTemp ~ year, data = sum_d, mean)

Where did temp_df come from? 
 Should it not be:
sum_d <- subset(d, d[,2]>=7 & d[,2] <=9)



More information about the R-help mailing list