间隔 R 内的周末日期

weekend dates within an interval R

我正在尝试确定周末是否在日期间隔内。我已经能够确定特定日期是否是周末,但在尝试查看日期范围时却无法确定。这可能吗?如果是这样,请指教。 TIA.

library(lubridate, chron)
start.date <- c("1/1/2017", "2/1/2017")
end.date   <- c("1/21/2017", "2/11/2017")

df <- data.frame(start.date, end.date)

df$start.date <- mdy(df$start.date)
df$end.date   <- mdy(df$end.date)

df$interval.date <- interval(df$start.date, df$end.date)

df$weekend.exist <- ifelse(is.weekend(df$interval.date), 1, 0)
# Error in dts - floor(dts) : 
#   Arithmetic operators undefined for 'Interval' and 'Interval' classes:
#   convert one to numeric or a matching time-span class.

您为什么不喜欢 seq 日期而不是创建间隔?喜欢

df$weekend.exist <- sapply(1:nrow(df), function(i) 
                as.numeric(any(is.weekend(seq(df$start.date[i], df$end.date[i],by = "day")))))
# [1] 1 1

library(dplyr)
df %>% 
group_by(start.date,end.date) %>%  
mutate(weekend.exist = as.numeric(any(is.weekend(seq(start.date, end.date,by = "day")))))
#   start.date   end.date weekend.exist
#      <date>     <date>         <dbl>
# 1 2017-01-01 2017-01-21             1
# 2 2017-02-01 2017-02-03             1