'How to calculate networkdays minus holidays between 2 dates

I am trying to understand how to apply a function like the NETWORKDAYS in Excel. I spent some time searching and found some examples but cannot get 1 to work.

Example 1 :

library(lubridate)
Date1 <- dmy("01/04/2017")
Date2 <- dmy("28/04/2017")
sum(!weekdays(seq(Date1, Date2, "days")) %in% c("Saturday", "Sunday"))

This works fine but need to remove holidays

Example 2 :

workdays = function(iniDate, endDate, holidays) {
  theDates = seq(from=iniDate,to=endDate,by="day")
  isHoliday = theDates %in% holidays
  isWeekend = (as.POSIXlt(theDates)$wday) %in% (c(0,6))
  return (sum(!isHoliday & !isWeekend))
}

This appears my best bet, however I cannot workout how to create bank holidays vector to apply to function.

How can I use this function, or is there a better way to calculate working days between 2 dates excluding holidays ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source