从这篇文章中扩展这个问题
Difference between two dates excluding weekends and given list of holidays in R
如果我有特定于州的假期怎么办?我如何通过州合并假期?
34
生成
holiday <- data.frame(h = as.Date(c("2016/05/3", "2016/05/3"),'%Y/%m/%d'),
s = c('state1','state2'),
stringsAsFactors=FALSE
)
d <- data.frame(d1 = as.Date(c('2016/05/2','2016/05/2'),'%Y/%m/%d'),
d2= as.Date(c('2016/05/10','2016/05/10'),'%Y/%m/%d'),
s = c('state1','state3'),
stringsAsFactors=FALSE)
链接中提供的解决方案是特定于日期的假日,但不考虑状态。我怎样才能融入国家。
所需状态是d中的一个额外列,它计算不包括特定于州的周末和假日的天数
答案 0 :(得分:1)
这是我的问题的一个不优雅的解决方案。希望你觉得这很有用。
library('dplyr')
holiday <- data.frame(h = as.Date(c("2016/05/3", "2016/05/3"),'%Y/%m/%d'),
s = c('state1','state2'),
stringsAsFactors=FALSE
)
d <- data.frame(d1 = as.Date(c('2016/05/2','2016/05/2'),'%Y/%m/%d'),
d2= as.Date(c('2016/05/10','2016/05/10'),'%Y/%m/%d'),
s = c('state1','state3'),
stringsAsFactors=FALSE)
state <- as.character(unique(d$s))
#function to exclude weekends and holidays
f <- function(a, b, h) {
d <- seq(a, b, 1)[-1]
sum(!format(d, "%u") %in% c("6", "7") & !d %in% h)
}
vf <- Vectorize(f, c("a", "b"))
#Function to calculate days excluding weekends and holidays by state
#state is done iteratively
datalist = list()
for (i in 1:length(state)) {
# ... make some data
#extract holidays by state as vectors
holi <- holiday %>%
filter(s== state[i]) %>%
select(h) %>%
pull()
#create new data by state
dat <- d %>%
filter(s == state[i])
dat$diff <- with(dat,vf(d1, d2, holi))
datalist[[i]] <- dat # add it to your list
rm('dat')
}
new_df = dplyr::bind_rows(datalist)
特定于州和不包括周末区分假期
d1 d2 s diff
1 2016-05-02 2016-05-10 state1 5
2 2016-05-02 2016-05-10 state3 6