Add business days without Saturday sunday and specific holidays in R

泄露秘密 提交于 2019-12-23 02:29:31

问题


I want to add days to a date in R, except Saturdays and Sundays and specific holidays. Suppose I have a dateset :

d <- dmy("25-7-2016","26-7-2016")
days <- c(3:4) 
data <- data.frame(d,days)
data

I want to add #days (days column) to a date (d column) I have tried the following code:

library(bizdays)
library(lubridate)
cal <- Calendar(weekdays=c('sunday', 'saturday'))
data$f <- offset(d, days, cal)
data

I can get the days without considering Saturdays and Sundays. But I want to exclude a specific holiday i.e. 27-7-2016. I tried to incorporate this specific holiday but getting an error. The code which I tried is as follows:

holiday <- dmy("27-7-2016")
cal <- Calendar(holiday,weekdays=c('sunday', 'saturday'))
data$f <- offset(d, days, cal)
data

Could you please help me to get is solved. Thanks in anticipation!


回答1:


Works if you add a start.date and end.date:

holiday <- dmy("27-7-2016")
cal <- Calendar(holidays = holiday,
                start.date = dmy("01-07-2016"),
                end.date = dmy("01-09-2016"), 
                weekdays=c('sunday', 'saturday'))
data$f <- offset(d, days, cal)
data

Gets you:

           d days          f
1 2016-07-25    3 2016-07-29
2 2016-07-26    4 2016-08-02

FYI, Calendar gives a warning message:

Warning message:
In Calendar(holidays = holiday, start.date = dmy("01-07-2016"),  :
This function will be deprecated, use create.calendar instead.


来源:https://stackoverflow.com/questions/38368964/add-business-days-without-saturday-sunday-and-specific-holidays-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!