capt-calculator capt-calculator - 28 days ago 5
R Question

R timeDate isHoliday() wont recognize custom dates

I am trying to use the isHoliday() function in R's timeDate package. I create a timeDate object called ppa_holidays, which is a list of holidays:

years <- c(2012,2013,2014,2015)
ppa_holidays <- timeDate(c(USNewYearsDay(year=years), Easter(year=years), USMemorialDay(year=years),
USIndependenceDay(year=years), USLaborDay(year=years), USThanksgivingDay(year=years),
USChristmasDay(year=years)))


Which gives:


> ppa_holidays
America/New_York
[1] [2012-01-01] [2013-01-01] [2014-01-01] [2015-01-01] [2012-04-08] [2013-03-31] [2014-04-20]
[8] [2015-04-05] [2012-05-28] [2013-05-27] [2014-05-26] [2015-05-25] [2012-07-04] [2013-07-04]
[15] [2014-07-04] [2015-07-04] [2012-09-03] [2013-09-02] [2014-09-01] [2015-09-07] [2012-11-22]
[22] [2013-11-28] [2014-11-27] [2015-11-26] [2012-12-25] [2013-12-25] [2014-12-25] [2015-12-25]


However, when I use isHoliday(), there are certain dates that return TRUE even though they are not in ppa_holidays, e.g.:


> isHoliday(timeDate("2012-12-08"), holidays=ppa_holidays)
2012-12-08 05:00:00
TRUE


What am I doing wrong here?

Answer

It seems the isHoliday function is also checking that the day is a weekday. By default it assumes Sat and Sun are "holidays" (or really non-business days) and 12/8/2012 was a Saturday. You can change the business days to include all the days of the week with the wday= parameter

isHoliday(timeDate("2012-12-08"), holidays=ppa_holidays, wday=0:6)
# 2012-12-08 
#      FALSE