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.
Pete Brecknock
2011-Feb-22 03:30 UTC
[R] Calculate a mean for several months for several years
For 5 years .... set.seed = 1 d=data.frame(year=rep(2007:2011,each=12), month=rep(1:12,5), meanTemp rnorm(60,10,5)) meanByMonth = ave(d$meanTemp, d$month, FUN = mean)[7:9] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Calculate-a-mean-for-several-months-for-several-years-tp3318363p3318501.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-Feb-22 03:44 UTC
[R] Calculate a mean for several months for several years
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-Ba@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]]
Reasonably Related Threads
- testing for temperature differences between years and sites?
- How to plot several years data with date information by months?
- ggplot2 date help (minor gridlines months and major grid lines years in a readable format)
- Extracting months and years from Dates while keeping order
- expanding 1:12 months to Jan:Dec