Exploratory Community

Getting sequence of business days between pairs of days

#1

Here is how to get sequence of business days between pairs of days in a data frame.

Let’s say you have a data frame which has pairs of start day and end day like this, and you want to get sequence of business days between those days, for each row of this data frame.

image

Since we are going to make use of bizdays package, we need to define a calendar for it first.
This can be done by a script like following, for example.

library(bizdays)
holidays <- as.character(c("2019-07-04", "2019-09-02", "2019-10-14")) #4th of July and Labor day and Columbus day

bizdays::create.calendar(name='MyCalendar', start.date="2000-01-01", end.date="2050-01-01",
                         holidays=holidays, weekdays=c('sunday', 'saturday'),
                         adjust.from=adjust.next, adjust.to=adjust.previous)
TRUE #This is just to pass validation of this script.

Now back to the data frame, group the rows of the data frame, though in this case one group consists of only one row.

Then, making use of do function and bizseq function in a custom R step like following, we can create a data frame with sequences of business days for each row of the original data frame.

do(data.frame(days=bizdays::bizseq(.$start, .$end, "MyCalendar")))

For your convenience to reproduce this, here is an Exploratory project which includes the script and steps described here.

https://www.dropbox.com/s/z7vh7o3vj3tx0y7/bizdays_example.zip?dl=1

I included another way to do it with nest/unnest and purrr::map function, which is a little more lengthy but more modern in this project too.

#2

Awesome work! Works perfectly…

except…

If your end date range is greater than the last date in your holiday calendar, bizseq returns a Date range error.

#3

Thank you for pointing that out, Tony!

I just updated the calendar definition to specify start.date and end.date explicitly, so that holidays does not have to cover the range of data.