How can I pad for weeks outside of the dates in the original data?
library(tidyverse)
df <- data.frame(x=c("2019-01-02",
"2019-01-02",
#"2019-01-03",
"2019-01-04",
"2019-01-04",
"2019-01-04",
"2019-01-09",
"2019-01-19"),
y=c(1, 0, 1, 1, 0, 1, 0))
# aggregate by week
df %>%
mutate(x = lubridate::ymd(as.character(x))) %>%
group_by(date = lubridate::floor_date(x, "1 week"))
# # A tibble: 7 x 3
# # Groups: date [3]
# x y date
# <date> <dbl> <date>
# 1 2019-01-02 1 2018-12-30
# 2 2019-01-02 0 2018-12-30
# 3 2019-01-04 1 2018-12-30
# 4 2019-01-04 1 2018-12-30
# 5 2019-01-04 0 2018-12-30
# 6 2019-01-09 1 2019-01-06
# 7 2019-01-19 0 2019-01-13
df %>%
mutate(x = lubridate::ymd(as.character(x))) %>%
group_by(date = lubridate::floor_date(x, "1 week")) %>%
count(name = "count") %>%
ungroup() %>%
padr::pad(interval = "week",
start_val = lubridate::ymd("2019-01-01"),
end_val = lubridate::ymd("2019-02-20")) %>%
replace(is.na(.), 0)
# Error: The specified interval is invalid for the datetime variable.
It works for day intervals:
df %>%
mutate(x = lubridate::ymd(as.character(x))) %>%
group_by(date = lubridate::floor_date(x, "1 day")) %>%
count(name = "count") %>%
ungroup() %>%
padr::pad(interval = "day",
start_val = lubridate::ymd("2019-01-01"),
end_val = lubridate::ymd("2019-02-20")) %>%
replace(is.na(.), 0)
The issue is the mismatch between the dates given by
floor_date()(e.g. 2018/12/30) and the period that you give topadr::pad()(e.g. 2019/01/01 to 2019/02/20).If you change them to be the same then it works.
For example: