I am working in R. I have a date sequence and I would like assign whether each particular date is term time or a school holiday. I plan to do this using a dataframe column where each row is labelled "Holiday" or "Term"
My approach is to create a time sequence and individually specify each holiday dates using a vector; the first element is the start date of the holiday and the second is the end of the holiday.
I then create an if-or statement to test whether the time lies within any of the holiday dates specified in the vectors.
Here is my code so far:
start <- as.POSIXlt("2015-10-10 00:00:00")
end <- as.POSIXlt("2016-03-31 00:00:00")
DateSeq <- seq(from=start, to=end, by="mins")
#Holidays defined using a vector with by start and end date
H1 <- c("2015-10-26", "2015-11-3") #October half term
H2 <- c("2015-12-16", "2016-01-05") #Christmas holiday
H3 <- c("2016-02-15", "2016-02-19") #Feb half term
H4 <- c("2016-03-24", "2016-03-31") #Easter holiday
date_table <- data.frame(Time = DateSeq)
if ((round(date_table$Time, units = "days")== H1[1] <> H1[2]) | (round(date_table$Time, units = "days") == H2[1] <> H2[2])) {
date_table$Holiday <- "Holiday"
} else {
date_table$Holiday <- "Term"
}
As you can see this code does not work and simply labels all the rows as "Term".
Therefore I am wondering the following:
How I can specify the date range in the holiday vectors so it can be used within the if statement?
Whether this is the best approach to use? I am quite new to R and I was thinking of alternatives such as creating a set of individual sequences for term and holidays and then stitching them together. This approach however seems fiddly but would appreciate your thoughts.
Thanks for your help.