如何在原始数据中的日期之外填充数周?
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.
它适用于天间隔:
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)