Padding multiple columns in a data frame or data table

661 Views Asked by At

I have a data frame like the following and would like to pad the dates. Notice that four days are missing for id 3.

df = data.frame(
   id = rep(1,1,1,2,2,3,3,3),
   date = lubridate::ymd("2017-01-01","2017-01-02","2017-01-03",
            "2017-05-10","2017-05-11","2017-01-03",
            "2017-01-08","2017-01-09"),
   type = c("A","A","A","B","B","C","C","C"),
   val1 = rnorm(8),
   val2 = rnorm(8))

df

I tried the padr package as I wanted a quick solution, but this doesn't seem to work.

?pad
padr::pad(df)
library(dplyr)
df %>% padr::pad(group = c('id'))
df %>% padr::pad(group = c('id','date'))

Any ideas on tools or other packages to pad a dataset across multiple columns and based on groupings

EDIT: So there are three missing dates in my df. "2017-01-03","2017-01-08","2017-01-09"

Thus, I want the final dates to include three extra rows that contain "2017-01-04","2017-01-05","2017-01-06","2017-01-07"

0

There are 0 best solutions below