Hi:

Here's one way:

# Manufacture some monthly data over 20 years
df <- data.frame(year = rep(1981:2000, each = 12),
                  month = factor(rep(month.abb, 20), levels = month.abb),
                  meantemp = rnorm(240,
                       mean = rep(c(30, 35, 40, 50, 60, 70, 75, 80, 75, 65,
50, 40), 12),
                       s = 2) )
# Subset the summer months
fdsumm <- subset(df, month %in% c('Jul', 'Aug', 'Sep'))
# Redefine the factor levels (months)
fdsumm$month <- factor(fdsumm$month, levels = c('Jul', 'Aug', 'Sep'))
# Find the monthly means
aggregate(meantemp ~ month, data = fdsumm, FUN = mean)

HTH,
Dennis


On Mon, Feb 21, 2011 at 4:07 PM, saba <sa...@gmx.net> wrote:

>
> Hello everyone,
>
> I have a dataset with 3 colums (Year, Month, MeanTemp). Now I would like to
> calculate the average of the mean temperature for the summer months (Juli,
> August, September) for each of the 20 years.
>
> I'm sure it's somehow possible with a loop, but all I tried so far didn't
> worked and by the time I spent looking for a solution I would have even be
> faster doing this in excel!!!
>
>
> Any help is greatly appreciated!!!
> Cheers
> Sandra
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Calculate-a-mean-for-several-months-for-several-years-tp3318363p3318363.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to