Generates all combinations of variables found in a dataset.

expand() is useful in conjunction with joins:

  • use with right_join() to convert implicit missing values to explicit missing values

  • use with anti_join() to find out which combinations are missing

nesting() is a helper that only finds combinations already present in the dataset.

expand(.df, ..., .name_repair = "check_unique", .by = NULL)

nesting(..., .name_repair = "check_unique")

Arguments

.df

A data.frame or data.table

...

Columns to get combinations of

.name_repair

Treatment of duplicate names. See ?vctrs::vec_as_names for options/details

.by

Columns to group by

Examples

df <- tidytable(x = c(1, 1, 2), y = c(1, 1, 2))

df %>%
  expand(x, y)
#> # A tidytable: 4 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     2
#> 3     2     1
#> 4     2     2

df %>%
  expand(nesting(x, y))
#> # A tidytable: 2 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     1
#> 2     2     2