Turns implicit missing values into explicit missing values.
This is a wrapper around expand()
,
dplyr::left_join()
and replace_na()
that's
useful for completing missing combinations of data.
complete(data, ..., fill = list())
data | A data frame. |
---|---|
... | Specification of columns to expand. Columns can be atomic vectors or lists. To find all unique combinations of x, y and z, including those not
found in the data, supply each variable as a separate argument.
To find only the combinations that occur in the data, use nest:
You can combine the two forms. For example,
For factors, the full set of levels (not just those that appear in the
data) are used. For continuous variables, you may need to fill in values
that don't appear in the data: to do so use expressions like
Length-zero (empty) elements are automatically dropped. |
fill | A named list that for each variable supplies a single value to
use instead of |
If you supply fill
, these values will also replace existing
explicit missing values in the data set.
library(dplyr, warn.conflicts = FALSE) df <- tibble( group = c(1:2, 1), item_id = c(1:2, 2), item_name = c("a", "b", "b"), value1 = 1:3, value2 = 4:6 ) df %>% complete(group, nesting(item_id, item_name))#> # A tibble: 4 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <int> <int> #> 1 1 1 a 1 4 #> 2 1 2 b 3 6 #> 3 2 1 a NA NA #> 4 2 2 b 2 5# You can also choose to fill in missing values df %>% complete(group, nesting(item_id, item_name), fill = list(value1 = 0))#> # A tibble: 4 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <dbl> <int> #> 1 1 1 a 1 4 #> 2 1 2 b 3 6 #> 3 2 1 a 0 NA #> 4 2 2 b 2 5