grouping by date ranges
date, r
Solution
Basically, what Ben said. Here's an example of what that'd look like in practice. (I've used a `Date` class object, since it sounds like you don't need the hour/minute/second info.)
date <- seq(from = as.Date("2010/5/30"), by="week", length=10) ## Example data
cuts <- seq(from = as.Date("2000/7/1"), by="year", length=13)
labs <- paste0("FY", 1:12)
cut(date, breaks = cuts, labels = labs)
# [1] FY10 FY10 FY10 FY10 FY10 FY11 FY11 FY11 FY11 FY11
# Levels: FY1 FY2 FY3 FY4 FY5 FY6 FY7 FY8 FY9 FY10 FY11 FY12
Problem
This would seem to be a simple task but I'm having difficulty executing it as well as finding examples of how it is done in R. I have a data frame with about 75K records. One field has dates and it goes back about 11 years. I've used the following code to strip off the hours minutes seconds so I just have year-month-day ``` dat$date=round.POSIXt(dat$date,units="day") ``` I am now trying to create a new field "FiscalYear" based on dates such that if the date is between 2008-07-01 and 2009-06-30 it gets assigned FY09...then between 2009-07-01 and 2010-06-30 it gets assigned to FY10, etc... The method I've been trying is with ifelse using >= && <= to set the date ranges but this isn't working. Any thoughts?