如何在另一个向量指定长度的向量中创建每个日期的相应序列

How to create consequative sequence of each date in a vector of length specified by another vector

dates <- "2018-07-14" "2018-04-19" "2019-08-15" "2018-12-04" "2018-05-02" "2019-04-14"
length< - c(2,4,3,5,1,3)

对于 1 的序列,我希望每个日期的长度都形成一个序列

"2018-07-14" "2018-07-15" "2018-04-19" "2018-04-20" "2018-04-21" "2018-04-22" "2019-08-15" "2019-08-16" "2019-08-17" "2018-12-04" "2018-12-05" "2018-12-06" "2018-12-07" "2018-12-08" "2018-05-02" "2019-04-14" "2019-04-15" "2019-04-16"

我们可以用Map循环对应的vector,用seq得到一天'date'by的序列,同时指定length.out 作为 'length'

中的每个值
do.call(c, Map(function(x, y) seq(x, length.out = y, by = '1 day'), 
       as.Date(dates), length))
#[1] "2018-07-14" "2018-07-15" "2018-04-19" "2018-04-20" "2018-04-21" "2018-04-22" "2019-08-15" "2019-08-16" "2019-08-17" "2018-12-04"
#[11] "2018-12-05" "2018-12-06" "2018-12-07" "2018-12-08" "2018-05-02" "2019-04-14" "2019-04-15" "2019-04-16"

数据

dates <- c( "2018-07-14", "2018-04-19", "2019-08-15" ,"2018-12-04", 
        "2018-05-02", "2019-04-14")
length<- c(2,4,3,5,1,3)