In tidyr::complete, I have to input variable names, how to avoid it? In actual, the variable are some many, it's boring to input one by one ... Anyone can help?
library(tidyverse)
raw_df <- data.frame(cat=c('A','B','C'),sub_category=c('1','2','3'))
Input variable name , the code run well
raw_df %>% tidyr::complete(cat,sub_category)
Below code can't work--the input way i wished
raw_df %>% tidyr::complete()
A slightly different approach would be to use expand_grid
and pass in all columns of the dataframe unpacked using !!!
via an anonymous function. This works but others may have more simple approaches to suggest!
library(tidyverse)
raw_df <- data.frame(cat=c('A','B','C'),sub_category=c('1','2','3'))
raw_df |>
(\(df) expand_grid(!!!df))()
#> # A tibble: 9 × 2
#> cat sub_category
#> <chr> <chr>
#> 1 A 1
#> 2 A 2
#> 3 A 3
#> 4 B 1
#> 5 B 2
#> 6 B 3
#> 7 C 1
#> 8 C 2
#> 9 C 3