R:两个日期之间的差异,不包括周末和给定的州特定假期列表

R: Difference between two dates excluding weekends and given list of holidays specific to state

从这个 post

扩展这个问题

Difference between two dates excluding weekends and given list of holidays in R

如果我有各州特定的假期怎么办。我如何按州合并假期?

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
          d1         d2      s
1 2016-05-02 2016-05-10 state1
2 2016-05-02 2016-05-10 state3
> holiday
           h      s
1 2016-05-03 state1
2 2016-05-03 state2

link 中提供的解决方案是特定于日期的假期,但不考虑各州。我怎样才能合并状态。

Desired state 是 d 中的一个额外列,它计算不包括特定于州的周末和节假日的天数

这是对我的问题的一个不雅的解决方案。希望你觉得这有用。

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