# Getting sequence of business days between pairs of days

**URL:** <https://community.exploratory.io/t/getting-sequence-of-business-days-between-pairs-of-days/1742>\
**Category:** Uncategorized\
**Created:** [September 4, 2019, 4:18am UTC](https://community.exploratory.io/t/getting-sequence-of-business-days-between-pairs-of-days/1742 "2019-09-04T04:18:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hideaki\_Hayashi](https://community.exploratory.io/user_avatar/community.exploratory.io/hideaki_hayashi/32/10_2.png) [@Hideaki\_Hayashi](https://community.exploratory.io/u/Hideaki_Hayashi)\
**Post date:** [September 4, 2019, 4:18am UTC](https://community.exploratory.io/t/getting-sequence-of-business-days-between-pairs-of-days/1742/1 "2019-09-04T04:18:12Z")

</div>

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](https://community.exploratory.io/uploads/default/original/1X/6056224652c06022ce5a5f21c8f1cd32d007e682.png)

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.

```auto
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.

 ![image](https://community.exploratory.io/uploads/default/original/1X/ab63b4c452e2d3294fbf5fe9ac9a6b5ffe9d2f8a.png)

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.

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

```

 ![image](https://community.exploratory.io/uploads/default/original/1X/bb4b719b101de16057855e4765353c93db45b42b.png)

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](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.

---

<div class="post-metadata">

**Author:** ![Tony\_Johnson](https://community.exploratory.io/user_avatar/community.exploratory.io/tony_johnson/32/330_2.png) [@Tony\_Johnson](https://community.exploratory.io/u/Tony_Johnson)\
**Post date:** [September 4, 2019, 5:52am UTC](https://community.exploratory.io/t/getting-sequence-of-business-days-between-pairs-of-days/1742/2 "2019-09-04T05:52:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hideaki\_Hayashi](https://community.exploratory.io/user_avatar/community.exploratory.io/hideaki_hayashi/32/10_2.png) [@Hideaki\_Hayashi](https://community.exploratory.io/u/Hideaki_Hayashi)\
**Post date:** [September 4, 2019, 4:19pm UTC](https://community.exploratory.io/t/getting-sequence-of-business-days-between-pairs-of-days/1742/3 "2019-09-04T16:19:39Z")

</div>

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.
