R 如何在两个日期之间填充日期

R 如何在两个日期之间填充日期,r,dataframe,date,data.table,sequence,R,Dataframe,Date,Data.table,Sequence,以下是我当前的数据帧的外观: df <- data.frame(name = c("A", "A", "A", "B", "B")), start_date = c("2020-01-23", "2019-10-15", "2019-07-28", "2020-03-15", "2019-04-2

以下是我当前的数据帧的外观:

df <- data.frame(name = c("A", "A", "A", "B", "B")), 
  start_date = c("2020-01-23", "2019-10-15", "2019-07-28", "2020-03-15", "2019-04-23")),
  end_date = c("2020-05-15", "2020-01-27", "2019-10-17", "2020-07-25", "2020-02-13")), 
  value = c(8.1, 3.3, 9.1, 9.4, 15.3)))



 name start_date   end_date value
    A 2020-01-23 2020-05-15     8
    A 2019-10-15 2020-01-27     3
    A 2019-07-28 2019-10-17     9
    B 2020-03-15 2020-07-25     9
    B 2019-04-23 2020-02-13    15
以下是我一直在尝试的:

 library(data.table)
 setDT(df) [, .(date = seq(as.Date(start_date), as.Date(end_date), by = "day")), by = end_date]
但是我得到了以下错误:

Error in seq.Date(as.Date(start_date), as.Date(end_date), by = "day") : 
  'from' must be of length 1

我该怎么做?我愿意使用其他包而不是data.table,如果它们工作得更好。

在这里,我们可能需要使用
by
作为行序列

library(data.table)
setDT(df)[, .(date = seq(as.Date(start_date), as.Date(end_date),
  by = 'day')), .(rn = seq_len(nrow(df)), name, value)][, rn := NULL][]

或者创建一个
列表
列,方法是循环“开始日期”、“结束日期”的相应元素,在
映射中创建一个日期序列
,然后在
列表

library(tidyr)
library(magrittr)
setDT(df)[, .(name, date = Map(seq, MoreArgs = list(by = '1 day'), 
      as.Date(start_date), as.Date(end_date)), value)] %>% 
   unnest(date)
# A tibble: 731 x 3
#   name  date       value
#   <chr> <date>     <dbl>
# 1 A     2020-01-23   8.1
# 2 A     2020-01-24   8.1
# 3 A     2020-01-25   8.1
# 4 A     2020-01-26   8.1
# 5 A     2020-01-27   8.1
# 6 A     2020-01-28   8.1
# 7 A     2020-01-29   8.1
# 8 A     2020-01-30   8.1
# 9 A     2020-01-31   8.1
#10 A     2020-02-01   8.1
# … with 721 more rows
library(tidyr)
图书馆(magrittr)
setDT(df)[,(名称,日期=地图(序号,MoreArgs=列表(by='1天'),
截止日期(开始日期)、截止日期(结束日期)),值)]%>%
unnest(日期)
#A tibble:731 x 3
#名称日期值
#         
#1A 2020-01-23 8.1
#2 A 2020-01-24 8.1
#3 A 2020-01-25 8.1
#4a 2020-01-26 8.1
#5 A 2020-01-27 8.1
#6 A 2020-01-28 8.1
#7 A 2020-01-29 8.1
#8 A 2020-01-30 8.1
#9 A 2020-01-31 8.1
#10 A 2020-02-01 8.1
#…还有721行

另一种使用
purr的方法

df <- data.frame(name = c("A", "A", "A", "B", "B"), 
  start_date = c("2020-01-23", "2019-10-15", "2019-07-28", "2020-03-15", "2019-04-23"),
  end_date = c("2020-05-15", "2020-01-27", "2019-10-17", "2020-07-25", "2020-02-13"), 
  value = c(8.1, 3.3, 9.1, 9.4, 15.3))

library(dplyr)
library(purrr)

# function take in the name, start, end, value and generate a df fill as wanted
generate_fill <- function(name, start, end, value) {
  tibble(name = name, 
    date = seq(as.Date(start), as.Date(end), by = "1 day"), 
    value = value)
}

# Map the function to original df and combine the result 
bind_rows(
  pmap(list(df[["name"]], df[["start_date"]], df[["end_date"]], df[["value"]]),
  generate_fill))
df
df <- data.frame(name = c("A", "A", "A", "B", "B"), 
  start_date = c("2020-01-23", "2019-10-15", "2019-07-28", "2020-03-15", "2019-04-23"),
  end_date = c("2020-05-15", "2020-01-27", "2019-10-17", "2020-07-25", "2020-02-13"), 
  value = c(8.1, 3.3, 9.1, 9.4, 15.3))

library(dplyr)
library(purrr)

# function take in the name, start, end, value and generate a df fill as wanted
generate_fill <- function(name, start, end, value) {
  tibble(name = name, 
    date = seq(as.Date(start), as.Date(end), by = "1 day"), 
    value = value)
}

# Map the function to original df and combine the result 
bind_rows(
  pmap(list(df[["name"]], df[["start_date"]], df[["end_date"]], df[["value"]]),
  generate_fill))
# A tibble: 731 x 3
   name  date       value
   <chr> <date>     <dbl>
 1 A     2020-01-23   8.1
 2 A     2020-01-24   8.1
 3 A     2020-01-25   8.1
 4 A     2020-01-26   8.1
 5 A     2020-01-27   8.1
 6 A     2020-01-28   8.1
 7 A     2020-01-29   8.1
 8 A     2020-01-30   8.1
 9 A     2020-01-31   8.1
10 A     2020-02-01   8.1
# … with 721 more rows