I'm trying to exclude the following dates when using bizdays()
in R: 2021-02-15 and 2021-02-16.
I have the following code:
library(bizdays)
library(lubridate)
create.calendar(name='MyCalendar', weekdays=c('sunday', 'saturday'),
adjust.from=adjust.next, adjust.to=adjust.previous, financial=F)
bizdays.options$set(default.calendar='MyCalendar')
Now, when I write, for example:
bizdays(ymd("2021-02-01"), ymd("2021-02-17"))
I get "13" as the output, as expected. However, I'm trying to exclude those two dates (2021-02-15 and 2021-02-16). Given that, I would like to get "11" as the output when I write bizdays(ymd("2021-02-01"), ymd("2021-02-17"))
.
Furthermore, I want to exclude 2021-02-15 and 2021-02-16 whenever I calculate the bussiness days between any date in February and/or other month. Thus, when I calculate bizdays(ymd("2021-02-01"), ymd("2021-02-28"))
I would like to get "18" instead of "20"; when I calculate bizdays(ymd("2021-01-23"), ymd("2021-02-28"))
I would like to get "23" instead of "25".
I have already tried including these dates as holidays inside a vector when using create.calendar()
:
create.calendar(name='MyCalendar', weekdays=c('sunday', 'saturday'),
adjust.from=adjust.next, adjust.to=adjust.previous, financial=F,
holidays = c(ymd("2021-02-15"), ymd("2021-02-16")))
bizdays.options$set(default.calendar='MyCalendar')
But when I write bizdays(ymd("2021-02-01"), ymd("2021-02-17"))
, I get the following error:
Error in bizdays.Date(ymd("2021-02-01"), ymd("2021-02-17")) :
Given date out of range.
How can I solve this?
Thanks!
Update: As mentioned in the comments of @Terru_theTerror 's reply, I still haven't found a reproducible alternative when I use create.calendar()
.
I finally solved it.
According to the R Documentation of the create.calendar()
function:
The arguments start.date and end.date can be set but once they aren't and holidays is set, start.date is defined to min(holidays) and end.date to max(holidays). If holidays isn't set start.date is set to '1970-01-01' and end.date to '2071-01-01'.
Thus, when creating the calendar, one should indeed include "2021-02-15" and "2021-02-16" as dates in the "holidays" vector. The thing is, one also has to specify the "start.date" and "end.date" arguments as they were by default:
create.calendar(name='MyCalendar', weekdays=c('sunday', 'saturday'),
adjust.from=adjust.next, adjust.to=adjust.previous, financial=F,
holidays = c(ymd("2021-02-15"), ymd("2021-02-16")),
start.date = ymd("1970-01-01"), end.date = ymd("2071-01-01"))
bizdays.options$set(default.calendar='MyCalendar')
This worked out perfectly!