Combine chosen variables of any class by concatenating them into one factor variable, and returns the result along with tidyverse code used to generate it.
Arguments
- data
a dataframe with the columns to be combined
- vars
a character vector of the variables to be combined
- sep
a character string to separate the levels
- name
a name for the new variable
- keep_empty
logical, if
FALSE
empty level combinations are removed from the factor- keep_na
logical, if
TRUE
the<NA>
in the factors orNA
in the characters will turn in a level"(Missing)"
; otherwise, the resulting entries will return<NA>
Value
original dataframe containing new columns of the new categorical variable with tidyverse code attached
Examples
combined <- combine_vars(warpbreaks, vars = c("wool", "tension"), sep = "_")
cat(code(combined))
#> warpbreaks |> dplyr::mutate(wool_tension = forcats::fct_cross(forcats::fct_na_value_to_level( wool, "(Missing)"), forcats::fct_na_value_to_level(tension, "(Missing)"), sep = "_", keep_empty = FALSE))
head(combined)
#> breaks wool tension wool_tension
#> 1 26 A L A_L
#> 2 30 A L A_L
#> 3 54 A L A_L
#> 4 25 A L A_L
#> 5 70 A L A_L
#> 6 52 A L A_L