I have these three intervals defined:
YEAR_1 <- interval(ymd(\'2002-09-01\'), ymd(\'2003-08-31\'))
YEAR_2 <- interval(ymd(\'2003-09-01\'), ymd(\'20
1st: create a data.table
contains all YEAR_N
> interval.dt <- data.table(Interval = c(YEAR_1, YEAR_2, YEAR_3))
> interval.dt
# Interval
#1: 2002-09-01 UTC--2003-08-31 UTC
#2: 2003-09-01 UTC--2004-08-31 UTC
#3: 2004-09-01 UTC--2005-08-31 UTC
2nd: define a function to get row index of interval.dt
when a specific year date fall in range interval.dt$Interval
using int_start(interval.dt$Interval) < year < int_end(interval.dt$Interval)
> findYearIndex <- function(year) {
interval.dt[,which(int_start(interval.dt$Interval) < year & year < int_end(interval.dt$Interval))]
}
3rd: sapply findYearIndex
function to every element in year date data.table
> dt <- data.table(year = df)
> dt$YearIndex <- paste("YEAR", sapply(dt$year, findYearIndex), sep = "_")
> dt
# year YearIndex
#1: 2003-06-11 YEAR_1
#2: 2004-08-11 YEAR_2
#3: 2004-06-03 YEAR_2
#4: 2004-01-20 YEAR_2
#5: 2005-02-25 YEAR_3
#6: 2003-01-07 YEAR_1
#7: 2003-11-19 YEAR_2
#8: 2003-03-12 YEAR_1
#9: 2003-12-29 YEAR_2
#10: 2003-03-26 YEAR_1
#11: 2004-08-19 YEAR_2
#12: 2004-07-19 YEAR_2
#13: 2003-04-29 YEAR_1
#14: 2003-05-06 YEAR_1
#15: 2005-10-27 YEAR_integer(0)
#ignore the rest of dt