[R] how to calculate seasonal mean for temperatures

arun smartpink111 at yahoo.com
Wed Aug 1 13:22:49 CEST 2012


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.




More information about the R-help mailing list