Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

date - R Create function to add water year column

I want to be able to create a water year column for a time series. The US water year is from Oct-Sept and is considered the year it ends on. For example the 2014 water year is from October 1, 2013 - September 30, 2014.

This is the US water year, but not the only water year. Therefore I want to enter in a start month and have a water year calculated for the date.

For example if my data looks like

        date
2008-01-01 00:00:00
2008-02-01 00:00:00
2008-03-01 00:00:00
2008-04-01 00:00:00
       .
       .
       .
2008-12-01 00:00:00

I want my function to work something like:

wtr_yr <- function(data, start_month) {

does stuff

}

Then my output would be

wtr_yr(data, 2)

         date                    wtr_yr
    2008-01-01 00:00:00           2008
    2008-02-01 00:00:00           2009 
    2008-03-01 00:00:00           2009
    2008-04-01 00:00:00           2009
           .
           .
           .
    2009-01-01 00:00:00           2009 
    2009-02-01 00:00:00           2010
    2009-03-01 00:00:00           2010
    2009-04-01 00:00:00           2010

I started by breaking the date up into separate columns, but I don't think that is the best way to go about it. Any advice?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can use POSIXlt to come up with an answer.

wtr_yr <- function(dates, start_month=9) {
  # Convert dates into POSIXlt
  dates.posix = as.POSIXlt(dates)
  # Year offset
  offset = ifelse(dates.posix$mon >= start_month - 1, 1, 0)
  # Water year
  adj.year = dates.posix$year + 1900 + offset
  # Return the water year
  adj.year
}

Let's now use this function in an example.

# Sample input vector
dates = c("2008-01-01 00:00:00",
"2008-02-01 00:00:00",
"2008-03-01 00:00:00",
"2008-04-01 00:00:00",
"2009-01-01 00:00:00",
"2009-02-01 00:00:00",
"2009-03-01 00:00:00",
"2009-04-01 00:00:00")

# Display the function output
wtr_yr(dates, 2)

# Combine the input and output vectors in a dataframe
df = data.frame(dates, wtr_yr=wtr_yr(dates, 2))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...