Learn R Programming

iNZightTools (version 1.13.0)

combineCatVars: Combine categorical variables into one

Description

Combine specified categorical variables by concatenating their values into one character, and returns the result along with tidyverse code used to generate it.

Usage

combineCatVars(
  .data,
  vars,
  sep = ".",
  name = paste(vars, collapse = sep),
  keep_empty = FALSE
)

Value

original dataframe containing a new column of the renamed categorical variable with tidyverse code attached

Arguments

.data

a dataframe with the columns to be combined

vars

a character vector of the categorical variables to be combined

sep

the separator to combine the values of the variables in var by. "." by default

name

a name for the new variable

keep_empty

logical, if FALSE empty level combinations are removed from the factor

Author

Owen Jin

Details

When either variable is NA, the result is NA.

Examples

Run this code
combined <- combineCatVars(warpbreaks, vars = c("wool", "tension"), sep = "_")
cat(code(combined))
head(combined)

Run the code above in your browser using DataLab