本文介绍了在R中添加不含周六周日和特定假日的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为R中的日期添加天,星期六和星期日以及特定的假日除外.假设我有一个日期集:

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

我想将#days(天列)添加到日期(d列)中,我尝试了以下代码:

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

我可以不用考虑周六和周日而获得美好的一天.但我想排除特定的假期,即2016年7月27日.我尝试合并这个特定的假期,但出现错误.我尝试过的代码如下:

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!

推荐答案

如果添加start.dateend.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

得到你:

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

FYI,Calendar给出警告消息:

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.

这篇关于在R中添加不含周六周日和特定假日的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:42