EdwinTh / padr

Padding of missing records in time series
https://edwinth.github.io/padr/
Other
132 stars 12 forks source link

How do you use padr::pad with multiple groups? #81

Closed kambanane closed 3 years ago

kambanane commented 3 years ago

Hi,

I can use pdrr::pad() with dates just fine, but what if I want to pad series in locations AND dates?

Here is some example code, which gives me the error: "x does not contain a variable of class Date, POSIXct, or POSIXlt."

Example

locations <- c("NYC", "NYC", "NYC", "NYC", "NYC", "SF", "SF", "SF", "SF", "SF") dates <- c( "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-03", "2020-01-05", "2020-01-02", "2020-01-04", "2020-01-04", "2020-01-05", "2020-01-06" ) df<- data.frame(cbind(as.Date(dates), locations)) names(df )<- c("dates", "locations")

df <- df %>% dplyr::group_by(locations, dates) %>% tally() %>% padr::pad() %>% fill_by_value()

EdwinTh commented 3 years ago

This is not because of the grouping, but because you use cbind in the df creation. This converts the date into a numeric. If you use df <- data.frame(dates = as.Date(dates), locations) instead, it works just fine.