Expand data frame to include all possible combinations of values
expand()
generates all combination of variables found in a dataset.
It is paired with nesting()
and crossing()
helpers. crossing()
is a wrapper around expand_grid()
that de-duplicates and sorts its inputs;
nesting()
is a helper that only finds combinations already present in the
data.
expand()
is often useful in conjunction with joins:
use it with right_join()
to convert implicit missing values to
explicit missing values (e.g., fill in gaps in your data frame).
use it with anti_join()
to figure out which combinations are missing
(e.g., identify gaps in your data frame).
expand(data, ..., .name_repair = "check_unique") crossing(..., .name_repair = "check_unique") nesting(..., .name_repair = "check_unique")
data |
A data frame. |
... |
Specification of columns to expand. Columns can be atomic vectors or lists.
When used with factors, When used with continuous variables, you may need to fill in values
that do not appear in the data: to do so use expressions like
|
.name_repair |
Treatment of problematic column names:
This argument is passed on as |
complete()
to expand list objects. expand_grid()
to input vectors rather than a data frame.
fruits <- tibble( type = c("apple", "orange", "apple", "orange", "orange", "orange"), year = c(2010, 2010, 2012, 2010, 2010, 2012), size = factor( c("XS", "S", "M", "S", "S", "M"), levels = c("XS", "S", "M", "L") ), weights = rnorm(6, as.numeric(size) + 2) ) # All possible combinations --------------------------------------- # Note that all defined, but not necessarily present, levels of the # factor variable `size` are retained. fruits %>% expand(type) fruits %>% expand(type, size) fruits %>% expand(type, size, year) # Only combinations that already appear in the data --------------- fruits %>% expand(nesting(type)) fruits %>% expand(nesting(type, size)) fruits %>% expand(nesting(type, size, year)) # Other uses ------------------------------------------------------- # Use with `full_seq()` to fill in values of continuous variables fruits %>% expand(type, size, full_seq(year, 1)) fruits %>% expand(type, size, 2010:2012) # Use `anti_join()` to determine which observations are missing all <- fruits %>% expand(type, size, year) all all %>% dplyr::anti_join(fruits) # Use with `right_join()` to fill in missing rows fruits %>% dplyr::right_join(all)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.