I think you are close: if there is a vector of days from 2017-04-01
to 2017-04-28
from:
allDays <- seq.Date(from=as.Date("2017-04-01"), to=as.Date("2017-04-28"), by=1)
And a vector of holidays from:
easter <- c(as.Date("2017-04-15"), as.Date("2017-04-16"), as.Date("2017-04-17"))
Then you can remove the holidays with the setdiff
function:
nonHolidays <- as.Date(setdiff(allDays, easter ), origin="1970-01-01")
Using a similar approach to your first example, weekends are returned with:
weekends <- nonHolidays[weekdays(nonHolidays) %in% c("Saturday", "Sunday")]
And the weekends can be removed from the non-holidays with setdiff
again:
nonHolidaysWeekends <- as.Date(setdiff(nonHolidays, weekends), origin="1970-01-01")
So you can wrap that in a function that returns the length of nonHolidaysWeekends
:
networkDays <- function(beginDate, endDate, holidayDates) {
# get all days between beginDate and endDate
allDays <- seq.Date(from=beginDate, to=endDate, by=1)
# use setdiff to remove holidayDates and convert back to date vector
nonHolidays <- as.Date(setdiff(allDays, holidayDates), origin="1970-01-01")
# find all weekends left in nonHolidays
weekends <- nonHolidays[weekdays(nonHolidays) %in% c("Saturday", "Sunday")]
# use setdiff again to remove the weekends from nonHolidays and convert back to date vector
nonHolidaysWeekends <- as.Date(setdiff(nonHolidays, weekends), origin="1970-01-01")
# return length of vector filtered for holidays and weekends
length(nonHolidaysWeekends)
}
d1 <- as.Date("2017-04-01")
d2 <- as.Date("2017-04-28")
# includes Easter Sunday
easter <- c(as.Date("2017-04-15"), as.Date("2017-04-16"), as.Date("2017-04-17"))
networkDays(d1, d2, easter)
There's a similar question here if that is of interest.