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: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:
This worked out perfectly!