library(dplyr, warn.conflicts = FALSE)
df <- tibble(
group = c(1:2, 1, 2),
item_id = c(1:2, 2, 3),
item_name = c("a", "a", "b", "b"),
value1 = c(1, NA, 3, 4),
value2 = 4:7
)
df
# Generate all possible combinations of `group`, `item_id`, and `item_name`
# (whether or not they appear in the data)
complete(df, group, item_id, item_name)
# Cross all possible `group` values with the unique pairs of
# `(item_id, item_name)` that already exist in the data
complete(df, group, nesting(item_id, item_name))
# Within each `group`, generate all possible combinations of
# `item_id` and `item_name` that occur in that group
df %>%
group_by(group) %>%
complete(item_id, item_name)
# You can also choose to fill in missing values. By default, both implicit
# (new) and explicit (pre-existing) missing values are filled.
complete(
df,
group,
nesting(item_id, item_name),
fill = list(value1 = 0, value2 = 99)
)
# You can limit the fill to only implicit missing values by setting
# `explicit` to `FALSE`
complete(
df,
group,
nesting(item_id, item_name),
fill = list(value1 = 0, value2 = 99),
explicit = FALSE
)
Run the code above in your browser using DataLab