Sam Albers
2012-Jun-11 17:22 UTC
[R] Define a variable on a non-standard year interval (Water Years)
Hello,
I am trying to define a different interval for a "year". In hydrology,
a "water year" is defined as the period between October 1st and
September 30 of the following year. I was wondering how I might do
this in R. Say I have a data.frame like the following and I want to
extract a variable with the water year specs as defined above:
df<-data.frame(Date=seq(as.Date("2000/10/1"),
as.Date("2003/9/30"), "days"))
## Extract the normal year
df$year <- factor(format(as.Date(df$Date), "%Y"))
So the question is how might I define a variable that extends from
October 1st to September 30th rather than the normal January 1st to
December 31st?
Thanks in advance!
Sam
MacQueen, Don
2012-Jun-11 19:56 UTC
[R] Define a variable on a non-standard year interval (Water Years)
Here's one way.
(not including the conversion to factor)
wdf <- data.frame(Date=seq(as.Date("2000/10/1"),
as.Date("2003/9/30"),
"days"))
wdf$wyr <- as.numeric(format(wdf$Date,'%Y'))
is.nxt <- as.numeric(format(wdf$Date,'%m')) %in% 1:9
wdf$wyr[ is.nxt ] <- wdf$wyr[is.nxt]-1
## and you can do some rudimentary checking with:
table(format(wdf$Date,'%Y'), wdf$wyr)
Note that df is the name of an R-supplied function and therefore not a
good choice for one's own use.
(By the way, thank you for providing easy to recreate example data.
As a minor point, this:
wdf <- data.frame(Date=seq(as.Date("2000/10/1"),
as.Date("2003/9/30"),
"3 weeks"))
would be just a little easier to work with for testing)
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 6/11/12 10:22 AM, "Sam Albers" <tonightsthenight at
gmail.com> wrote:
>Hello,
>
>I am trying to define a different interval for a "year". In
hydrology,
>a "water year" is defined as the period between October 1st and
>September 30 of the following year. I was wondering how I might do
>this in R. Say I have a data.frame like the following and I want to
>extract a variable with the water year specs as defined above:
>
>df<-data.frame(Date=seq(as.Date("2000/10/1"),
as.Date("2003/9/30"),
>"days"))
>
>## Extract the normal year
>df$year <- factor(format(as.Date(df$Date), "%Y"))
>
>So the question is how might I define a variable that extends from
>October 1st to September 30th rather than the normal January 1st to
>December 31st?
>
>Thanks in advance!
>
>Sam
>
>______________________________________________
>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.
Gabor Grothendieck
2012-Jun-11 20:05 UTC
[R] Define a variable on a non-standard year interval (Water Years)
On Mon, Jun 11, 2012 at 1:22 PM, Sam Albers <tonightsthenight at gmail.com> wrote:> Hello, > > I am trying to define a different interval for a "year". In hydrology, > a "water year" is defined as the period between October 1st and > September 30 of the following year. I was wondering how I might do > this in R. Say I have a data.frame like the following and I want to > extract a variable with the water year specs as defined above: > > df<-data.frame(Date=seq(as.Date("2000/10/1"), as.Date("2003/9/30"), "days")) > > ## Extract the normal year > df$year <- factor(format(as.Date(df$Date), "%Y")) > > So the question is how might I define a variable that extends from > October 1st to September 30th rather than the normal January 1st to > December 31st? >See this: https://stat.ethz.ch/pipermail/r-help/2004-June/052952.html -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com