R: expand and fill data frame by date in series

醉酒当歌 提交于 2021-02-05 11:24:27

问题


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 data frame:

      date   group
1 2018-11-28     A
2 2018-12-17     B
3 2019-01-23     C

I want to have a data frame that expand the date to next column but still keep the group information. For example, date from 2018-11-28 to 2018-12-16 is with group A, date from 2018-12-17 to 2019-01-22 is with group B and 2019-01-23 is with group C.

This is the output (result_df) I want:

time=c(seq(as.Date("2018-11-28"), as.Date("2018-12-17")-1, by=1), 
seq(as.Date("2018-12-17"), as.Date("2019-01-23")-1, by=1),as.Date("2019-01-23") )
group1=c(rep("A",as.numeric(as.Date("2018-12-17")-as.Date("2018-11-28"))), 
rep("B",as.numeric(as.Date("2019-01-23")-as.Date("2018-12-17"))), "C" )
result_df=data.frame(time,group1 )
result_df

I am wondering if there is any more efficient way (using dplyr) to handle this issue.

Thanks in advance.


回答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)


来源:https://stackoverflow.com/questions/54374134/r-expand-and-fill-data-frame-by-date-in-series

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!