根据开始日期创建重叠日期向量

Create overlapping date vector based on start date

只是想看看是否有更直观的方法来做到这一点。我需要创建从特定开始日期到当前日期的重叠日期间隔。例如,我的第一个时间间隔是 (2019-01-01 -- 2019-04-30),而我的下一个时间间隔是 (2019-03-01 -- 2019-05-31),这样就有重叠至少1个月。

这是我的代码:

library(dplyr)
library(lubridate)
current_date <- today()
start_date <- as.Date("2019-01-01")
i <- NULL
interval_vector <- c() # to store
for(i in 1:length(months_sequence)){
  # define end date (90 days)
  end_date <- ceiling_date(start_date + (days(90)),unit = "month") - 1
  interval_vector[[i]] <- paste0(start_date, " ", end_date)
  # define new start date
  start_date <- floor_date(end_date - days(30), unit = "month")
  # drop future dates
  interval_vector <- interval_vector[interval_vector < current_date]
  # interval_vector <- interval_vector
}
interval_vector

结果如下:

[1] "2019-01-01 2019-04-30" "2019-03-01 2019-05-31" "2019-05-01 2019-07-31" "2019-07-01 2019-09-30" "2019-08-01 2019-10-31"
[6] "2019-10-01 2019-12-31" "2019-12-01 2020-02-29" "2020-01-01 2020-03-31" "2020-03-01 2020-05-31" NA                     
[11] NA                      NA                      NA                      NA                      NA                     
[16] NA  

两个问题:

  1. 有更好的方法吗?
  2. 为什么它会返回 NA,我该如何删除它们?

谢谢大家

lubridate 有一个间隔函数。参考:https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html

例子

library(lubridate)

t <- as.POSIXct("2019-01-01", tz = "UTC")
n <- 16
arrive <- seq(t, by = "month", length.out = n)
depart <- tail(seq(t, by = "month", length.out = n+2), -2)
interval(arrive, depart)

结果

 [1] 2019-01-01 UTC--2019-03-01 UTC 2019-02-01 UTC--2019-04-01 UTC
 [3] 2019-03-01 UTC--2019-05-01 UTC 2019-04-01 UTC--2019-06-01 UTC
 [5] 2019-05-01 UTC--2019-07-01 UTC 2019-06-01 UTC--2019-08-01 UTC
 [7] 2019-07-01 UTC--2019-09-01 UTC 2019-08-01 UTC--2019-10-01 UTC
 [9] 2019-09-01 UTC--2019-11-01 UTC 2019-10-01 UTC--2019-12-01 UTC
[11] 2019-11-01 UTC--2020-01-01 UTC 2019-12-01 UTC--2020-02-01 UTC
[13] 2020-01-01 UTC--2020-03-01 UTC 2020-02-01 UTC--2020-04-01 UTC
[15] 2020-03-01 UTC--2020-05-01 UTC 2020-04-01 UTC--2020-06-01 UTC

或者,在没有 lubridate 的情况下,简单的粘贴将完全按照您的描述创建字符向量

paste(format(arrive, "%F"), format(depart, "%F"), sep = " ")

结果

 [1] "2019-01-01 2019-03-01" "2019-02-01 2019-04-01" "2019-03-01 2019-05-01"
 [4] "2019-04-01 2019-06-01" "2019-05-01 2019-07-01" "2019-06-01 2019-08-01"
 [7] "2019-07-01 2019-09-01" "2019-08-01 2019-10-01" "2019-09-01 2019-11-01"
[10] "2019-10-01 2019-12-01" "2019-11-01 2020-01-01" "2019-12-01 2020-02-01"
[13] "2020-01-01 2020-03-01" "2020-02-01 2020-04-01" "2020-03-01 2020-05-01"
[16] "2020-04-01 2020-06-01"