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
You can use walk
from package purrr
for this:
purrr::walk(1:3, ~(df$Year[as.POSIXlt(df$DATE) %within% get(paste0("YEAR_", .))] <<- .))
or maybe you should write a loop to improve readability (unless taboo for you):
df$YR <- NA
for(i in 1:3){
interval <- get(paste0("YEAR_", i))
index <-which(as.POSIXlt(df$DATE) %within% interval)
df$YR[index] <- i
}