Search code examples
rdatetimedata-sciencebizdays

How to include "from" day in bizdays() in R


Let's say I do the following:

create.calendar(name='MyCalendar', weekdays=c('sunday', 'saturday'),
            adjust.from=adjust.next, adjust.to=adjust.previous)
bizdays.options$set(default.calendar='MyCalendar')

Now, I want to calculate the number of business days between 2020-11-23 and 2020-11-27 (including November 23rd and 27th).

However, when I use bizdays():

bizdays("2020-11-23", "2020-11-27")

The output is "4". I would like to get a "5" to include all days. How can I achieve this?

Thanks!


Solution

  • We want to include from only if it's a business day i.e is.bizday(from)==TRUE thus we can make up the following function:

    library(bizdays)
    bizdays_inc <- function (from, to, cal = bizdays.options$get("default.calendar")) 
    {
        is.bizday(from) + bizdays(from, to, cal)
    }
    bizdays_inc("2020-11-23", "2020-11-27")
    [1] 5
    

    Or if we set the financial flag to FALSE in the calendar:

    create.calendar(name='MyCalendar', weekdays=c('sunday', 'saturday'),
                adjust.from=adjust.next, adjust.to=adjust.previous, financial=F)
    bizdays.options$set(default.calendar='MyCalendar')
    
    bizdays("2020-11-23", "2020-11-27")
    [1] 5