Hello everybody, I need to calculate seasonal means with temperature data for my work. I have 70 files coming from weather stations, which looks like this for example: startdate <- as.POSIXct("01/01/2006", format = "%d/%m/%Y") enddate <- as.POSIXct("05/01/2006", format = "%d/%m/%Y") date <- seq(from = startdate, to = enddate, by = "days",format = "%d/%m/%Y") DF <- data.frame(data=c(2.5,1.4,3.6,0.5,-1.2),date=date) With this daily data, I need to calculate seasonal means. I mean for season: winter (January,February,March) ; Spring (April,May,June) ; Summer(July,August,September) and Autumn(October,November,December). My main problem is that all my files starts and ends not the same year (some of them starts 1st January 2006 and ends 31th december 2008, some of them starts 1st January 2007 and ends 31th December 2011, ...). So not the same year, but all of them starts a 1st January and ends a 31th December. I'd like first to delete (or ignore) all the first 2 months (January and February) and the last month (December) of all my files, because I cannot calculate a seasonal means for them (not all the 3 months). But the problem for the first 2 months is for leap yars (with 29th February). For example, if my file starts in 2008, the first 2 months will not be the same length as files starting in 2007 or 2006. So I cannot just delete the first lines of my files because there'll be a problem for these leap years. And then, I'd like to calculate my seasonal means on each 3 months (like I showed you before). For example, my object "seasonal means" should look like this: Spring 2006: xx ; Summer 2006: xx, ....... (with xx my seasonal means). Have you any idea how to do this? I found functions such like "xts()" but it need to specify a year, so in my case it couldn't work. I need to automatize this for all my files, so it shouldn't depend on the start year. Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-seasonal-mean-for-temperatures-tp4638639.html Sent from the R help mailing list archive at Nabble.com.
Hi Something like aggregate(DF$data, list(quarters(DF$date), format(DF$date, "%Y")), mean) Regards Petr> > Hello everybody, > > I need to calculate seasonal means with temperature data for my work. > I have 70 files coming from weather stations, which looks like this for > example: > > startdate <- as.POSIXct("01/01/2006", format = "%d/%m/%Y") > enddate <- as.POSIXct("05/01/2006", format = "%d/%m/%Y") > date <- seq(from = startdate, to = enddate, by = "days",format ="%d/%m/%Y")> > DF <- data.frame(data=c(2.5,1.4,3.6,0.5,-1.2),date=date) > > With this daily data, I need to calculate seasonal means. > I mean for season: winter (January,February,March) ; Spring(April,May,June)> ; Summer(July,August,September) and Autumn(October,November,December). > > My main problem is that all my files starts and ends not the same year(some> of them starts 1st January 2006 and ends 31th december 2008, some ofthem> starts 1st January 2007 and ends 31th December 2011, ...). > > So not the same year, but all of them starts a 1st January and ends a31th> December. > > I'd like first to delete (or ignore) all the first 2 months (January and > February) and the last month (December) of all my files, because Icannot> calculate a seasonal means for them (not all the 3 months). > But the problem for the first 2 months is for leap yars (with 29th > February). For example, if my file starts in 2008, the first 2 monthswill> not be the same length as files starting in 2007 or 2006. So I cannotjust> delete the first lines of my files because there'll be a problem forthese> leap years. > And then, I'd like to calculate my seasonal means on each 3 months (likeI> showed you before). > For example, my object "seasonal means" should look like this: Spring2006:> xx ; Summer 2006: xx, ....... (with xx my seasonal means). > > Have you any idea how to do this? I found functions such like "xts()"but it> need to specify a year, so in my case it couldn't work. I need toautomatize> this for all my files, so it shouldn't depend on the start year. > Thanks a lot! > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to- > calculate-seasonal-mean-for-temperatures-tp4638639.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Here is my approximation: # Creation of the temporal variables DF$year <- as.numeric(format(DF$date, format = "%Y")) DF$month <- as.numeric(format(DF$date, format = "%m")) # For years with data from 2006 to 2008 DF_type1 <- DF [ - which (year == 2006 & month ==1 | year == 2006 & month =2 | year == 2008 & month == 12), ] # For years with data from 2007 to 2011 DF_type2 <- DF [ - which (year == 2007 & month ==1 | year == 2007 & month =2 | year == 2011 & month == 12), ] # Including the Season as a factor DF$season <- factor ( with ( ifelse (( month == 1 | nonth == 2 | month == 3 ), "Win", ifelse ((month == 4 | nonth == 5 | month == 6 ) , "Spr", ifelse ((month == 6 | nonth == 7 | month == 8 ) , "Sum", "Aut"))))) # To get the mean per year and season library (plyr) ddply ( DF, . (year, season), summarize, mean_season = mean (data)) -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-seasonal-mean-for-temperatures-tp4638639p4638649.html Sent from the R help mailing list archive at Nabble.com.
HI, Try this: #This might get you started: startdate <- as.POSIXct("01/01/2006", format = "%d/%m/%Y") enddate <- as.POSIXct("01/12/2006", format = "%d/%m/%Y") ?date <- seq(from = startdate, to = enddate, by = "months",format = "%d/%m/%Y") ?DF <- data.frame(data=c(2.5,1.4,3.6,0.5,-1.2,6.5,7,4.5,5.8,3.1,1.5,2.8),date=date) DF1<-within(DF,{datequart<-format(as.yearqtr(date,"%q"))}) aggregate(data~datequart, data=DF1,mean) ? datequart???? data 1?? 2006 Q1 2.500000 2?? 2006 Q2 1.933333 3?? 2006 Q3 5.766667 4?? 2006 Q4 2.466667 A.K. ----- Original Message ----- From: jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> To: r-help at r-project.org Cc: Sent: Wednesday, August 1, 2012 4:57 AM Subject: [R] how to calculate seasonal mean for temperatures Hello everybody, I need to calculate seasonal means with temperature data for my work. I have 70 files coming from weather stations, which looks like this for example: startdate <- as.POSIXct("01/01/2006", format = "%d/%m/%Y") enddate <- as.POSIXct("05/01/2006", format = "%d/%m/%Y") date <- seq(from = startdate, to = enddate, by = "days",format = "%d/%m/%Y") DF <- data.frame(data=c(2.5,1.4,3.6,0.5,-1.2),date=date) With this daily data, I need to calculate seasonal means. I mean for season: winter (January,February,March) ; Spring (April,May,June) ; Summer(July,August,September) and Autumn(October,November,December). My main problem is that all my files starts and ends not the same year (some of them starts 1st January 2006 and ends 31th december 2008, some of them starts 1st January 2007 and ends 31th December 2011, ...). So not the same year, but all of them starts a 1st January and ends a 31th December. I'd like first to delete (or ignore) all the first 2 months (January and February) and the last month (December) of all my files, because I cannot calculate a seasonal means for them (not all the 3 months). But the problem for the first 2 months is for leap yars (with 29th February). For example, if my file starts in 2008, the first 2 months will not be the same length as files starting in 2007 or 2006. So I cannot just delete the first lines of my files because there'll be a problem for these leap years. And then, I'd like to calculate my seasonal means on each 3 months (like I showed you before). For example, my object "seasonal means" should look like this: Spring 2006: xx ; Summer 2006: xx, ....... (with xx my seasonal means). Have you any idea how to do this? I found functions such like "xts()" but it need to specify a year, so in my case it couldn't work. I need to automatize this for all my files, so it shouldn't depend on the start year. Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-seasonal-mean-for-temperatures-tp4638639.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
It's working now! The problem was not for winter, but with the "with" you had in your object "DF$season. I got an error: invalid 'envir' argument. I removed it and now it seems to be OK. Thank you very much for your help ricardo. -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-seasonal-mean-for-temperatures-tp4638639p4638670.html Sent from the R help mailing list archive at Nabble.com.