问题
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