Add rows to df to complete all combinations of columns .x and .y. Importantly, this function observes and maintains any groups created by dplyr::group_by().

add_missing_combinations(df, .x, .y)

Arguments

df

a data frame (or tibble) object

.x, .y

column names to make combinations of

Value

a data frame (or tibble) with additional columns

Examples

df <- data.frame( a = c("A", "B"), b = c("C", "D"), untouched = c(1, 2) ) df
#> a b untouched #> 1 A C 1 #> 2 B D 2
add_missing_combinations(df, a, b)
#> a b untouched #> 1 A C 1 #> 2 B D 2 #> 3 A A NA #> 4 B A NA #> 5 C A NA #> 6 D A NA #> 7 A B NA #> 8 B B NA #> 9 C B NA #> 10 D B NA #> 11 B C NA #> 12 C C NA #> 13 D C NA #> 14 A D NA #> 15 C D NA #> 16 D D NA