1

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"

www
  • 38,575
  • 12
  • 48
  • 84
ATMA
  • 1,450
  • 4
  • 23
  • 33
  • i think you want `nalocf` from `zoo` – MichaelChirico Jan 08 '18 at 22:06
  • I don't follow this example clearly. the `rep(1,1,1,2,2,3,3,3)` evaluates to `1,1` repeated. There are no missing values from the 8 by 5 resultant data frame. Can you explicitly state or show in a data frame printout what it is exactly that you are trying to achieve? – Kamil Jan 08 '18 at 22:17
  • I think the problem lies in the way you create your data. As Kamil indicated, you use `rep` incorrectly. Also make sure it has the dates as you think they should have. – Edwin Jan 29 '18 at 08:21

0 Answers0