R: expand and fill data frame by date in series

后端 未结 1 1355
小蘑菇
小蘑菇 2021-01-28 05:56

I have the raw data frame:

igroup=c(\"A\", \"B\", \"C\")
demo_df=data.frame(date=c(\"2018-11-28\", \"2018-12-17\", \"2019-01-23\"), group)

Raw

相关标签:
1条回答
  • First, make sure date is stored as a date object:

    demo_df$date <- as.Date(demo_df$date, format = "%Y-%m-%d")
    

    Then using tidyverse, we first complete the sequence, then fill the group down:

    library(tidyverse)
    
    demo_df %>% complete(date = seq.Date(min(date), max(date), by = "day")) %>% 
     fill(igroup)
    
    0 讨论(0)
提交回复
热议问题