Мы можем использовать complete
для генерации всех дат от минимального day
до максимального значения в day
, а затем right_join
с calendar
, чтобы сохранить только даты, присутствующие в calendar
.
library(dplyr)
df %>%
mutate(day = as.Date(day)) %>%
tidyr::complete(item, sale, day = seq(min(day), max(day), by = 'day'),
fill = list(value = 0)) %>%
right_join(calendar %>% mutate(day = as.Date(day)), by = 'day')
# A tibble: 20 x 4
# item sale day value
# <fct> <fct> <date> <dbl>
# 1 apple no 2011-01-01 0
# 2 apple yes 2011-01-01 100
# 3 banana no 2011-01-01 0
# 4 banana yes 2011-01-01 0
# 5 apple no 2011-01-02 200
# 6 apple yes 2011-01-02 0
# 7 banana no 2011-01-02 0
# 8 banana yes 2011-01-02 0
# 9 apple no 2011-01-04 0
#10 apple yes 2011-01-04 0
#11 banana no 2011-01-04 0
#12 banana yes 2011-01-04 0
#13 apple no 2011-01-05 0
#14 apple yes 2011-01-05 0
#15 banana no 2011-01-05 0
#16 banana yes 2011-01-05 0
#17 apple no 2011-01-06 0
#18 apple yes 2011-01-06 0
#19 banana no 2011-01-06 0
#20 banana yes 2011-01-06 500
data
df <- structure(list(day = structure(1:3, .Label = c("2011-01-01",
"2011-01-02", "2011-01-06"), class = "factor"), item = structure(c(1L,
1L, 2L), .Label = c("apple", "banana"), class = "factor"), sale =
structure(c(2L, 1L, 2L), .Label = c("no", "yes"), class = "factor"),
value = c(100L, 200L, 500L)), class = "data.frame", row.names = c("1", "2", "4"))
calendar <- structure(list(day = structure(1:5, .Label = c("2011-01-01",
"2011-01-02", "2011-01-04", "2011-01-05", "2011-01-06"), class =
"factor")), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))