Clint Bowman
2004-Jun-16 18:21 UTC
[R] Aggregating on Water Year Rather Than Calendar Year
The US water year extends from 01 October yyyy-1 through 30 September yyyy and is referenced by the year starting on the included 01 January yyyy. I'd like to be able to find the annual means for the water year. To do so I've taken the input date-time, which is in the usual format "1991-10-07 10:35:00" changed it by: w$d<-as.POSIXct(w$date.time) Now I can add an offset of 92 days w$w.year<-as.POSIXct(w$d+7948800) and have the years correspond to the "water year." Now I wish to obtain some means by something like: waterT<-aggregate(w$value[w$param.name=="Temperature"], list(w$w.year[w$param.name=="Temperature"]),mean) Except that I need to work on the year and being a neophyte in date arithmetic I'm not finding a working method. TIA Clint -- Clint Bowman INTERNET: clint at ecy.wa.gov Air Quality Modeler INTERNET: clint at math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600 FAX: (360) 407-7534 Olympia, WA 98504-7600
Don MacQueen
2004-Jun-16 20:57 UTC
[R] Aggregating on Water Year Rather Than Calendar Year
It's not clear where your problem is. Did w$w.year come out wrong? Or did the aggregate() function fail? For getting the water year, I would do it differently: (this is for a U.S. English locale, and minimally tested) tmp <- as.POSIXlt(w$d)$year+1900 w$w.year <- ifelse( format(w$d,'%b') %in% c('Oct','Nov','Dec'), tmp+1, tmp) Then you can do things like table(w$w.year) table(w$w.year,w$param.name) to help find out if w.year came out like it should. You'll have to provide error messages or something if the problem is with using aggregate(). At 11:21 AM -0700 6/16/04, Clint Bowman wrote:>The US water year extends from 01 October yyyy-1 through 30 September yyyy >and is referenced by the year starting on the included 01 January yyyy. >I'd like to be able to find the annual means for the water year. To do so >I've taken the input date-time, which is in the usual format > >"1991-10-07 10:35:00" > >changed it by: > >w$d<-as.POSIXct(w$date.time) > >Now I can add an offset of 92 days > >w$w.year<-as.POSIXct(w$d+7948800)Try w$w.year <- w$d+7948800> >and have the years correspond to the "water year." > >Now I wish to obtain some means by something like: > >waterT<-aggregate(w$value[w$param.name=="Temperature"], >list(w$w.year[w$param.name=="Temperature"]),mean) > >Except that I need to work on the year and being a neophyte in date >arithmetic I'm not finding a working method. > >TIA > >Clint > >-- >Clint Bowman INTERNET: clint at ecy.wa.gov >Air Quality Modeler INTERNET: clint at math.utah.edu >Department of Ecology VOICE: (360) 407-6815 >PO Box 47600 FAX: (360) 407-7534 >Olympia, WA 98504-7600 >-Don -- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Gabor Grothendieck
2004-Jun-17 00:53 UTC
[R] Aggregating on Water Year Rather Than Calendar Year
Try this to get the US water year: require(chron) with(month.day.year(unclass(as.Date(w$date.time))), year+(month>9)) Date: Wed, 16 Jun 2004 11:21:56 -0700 (PDT) From: Clint Bowman <clint at ecy.wa.gov> To: <r-help at stat.math.ethz.ch> Subject: [R] Aggregating on Water Year Rather Than Calendar Year The US water year extends from 01 October yyyy-1 through 30 September yyyy and is referenced by the year starting on the included 01 January yyyy. I'd like to be able to find the annual means for the water year. To do so I've taken the input date-time, which is in the usual format "1991-10-07 10:35:00" changed it by: w$d<-as.POSIXct(w$date.time) Now I can add an offset of 92 days w$w.year<-as.POSIXct(w$d+7948800) and have the years correspond to the "water year." Now I wish to obtain some means by something like: waterT<-aggregate(w$value[w$param.name=="Temperature"], list(w$w.year[w$param.name=="Temperature"]),mean) Except that I need to work on the year and being a neophyte in date arithmetic I'm not finding a working method. TIA
Gabor Grothendieck
2004-Jun-17 04:32 UTC
[R] Aggregating on Water Year Rather Than Calendar Year
And here is a slightly shorter variant: with(as.POSIXlt(w$date.time), 1900+year+(mon>8) ) --- Gabor Grothendieck <ggrothendieck at myway.com>: Try this to get the US water year: require(chron) with(month.day.year(unclass(as.Date(w$date.time))), year+(month>9)) Date: Wed, 16 Jun 2004 11:21:56 -0700 (PDT) From: Clint Bowman <clint at ecy.wa.gov> To: <r-help at stat.math.ethz.ch> Subject: [R] Aggregating on Water Year Rather Than Calendar Year The US water year extends from 01 October yyyy-1 through 30 September yyyy and is referenced by the year starting on the included 01 January yyyy. I'd like to be able to find the annual means for the water year. To do so I've taken the input date-time, which is in the usual format "1991-10-07 10:35:00" changed it by: w$d<-as.POSIXct(w$date.time) Now I can add an offset of 92 days w$w.year<-as.POSIXct(w$d+7948800) and have the years correspond to the "water year." Now I wish to obtain some means by something like: waterT<-aggregate(w$value[w$param.name=="Temperature"], list(w$w.year[w$param.name=="Temperature"]),mean) Except that I need to work on the year and being a neophyte in date arithmetic I'm not finding a working method. TIA