I have a field of dates that mark the end of weeks. I am trying to create a new field using a function that flags (1 or 0) whether that week included any of 6 holidays specified using the timeDate package. I am getting the following error: "Error during wrapup: comparison (3) is possible only for atomic and list types" - how can I resolve?
The function should take the end-of-week date (x) in the format yyyy-mm-dd (e.g., 2017-01-01) and the year of that date (y) in the format yyyy (e.g., 2017).
library(lubridate)
library(timeDate)
Date = as.Date(c("2017-01-01", "2017-01-08", "2017-01-15", "2017-01-22", "2017-06-04", "2017-07-09", "2017-07-16"))
Year = year(Date)
Holiday.During.Week = as.Date(c("2017-01-01", NA, NA, NA, "2017-05-29", "2017-07-04", NA))
Desired.Output = c(1,0,0,0,1,1,0)
data <- data.frame(Date, Year, Holiday.During.Week, Desired.Output)
data
holiday.function = function(x, y) {
return(
as.numeric(
(USNewYearsDay(y) < x & USNewYearsDay(y) > (x - 7)) +
(USMemorialDay(y) < x & USMemorialDay(y) > (x - 7)) +
(USIndependenceDay(y) < x & USIndependenceDay(y) > (x - 7)) +
(USLaborDay(y) < x & USLaborDay(y) > (x - 7)) +
(USThanksgivingDay(y) < x & USThanksgivingDay(y) > (x - 7)) +
(USChristmasDay(y) < x & USChristmasDay(y) > (x - 7))
)
)
}
data$Holiday.Flag = holiday.function(data$Date, data$Year)
Edit: thanks to Ian Campbell for working on this without provided data. I've updated the code to include a sample data frame and libraries