combine_vars: Combine variables into one categorical variable

View source: R/mutate.R

combine_varsR Documentation

Combine variables into one categorical variable

Description

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.

Usage

combine_vars(
  data,
  vars,
  sep = ":",
  name = NULL,
  keep_empty = FALSE,
  keep_na = TRUE
)

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 or NA 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

Author(s)

Owen Jin, Zhaoming Su

Examples

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


iNZightTools documentation built on Oct. 12, 2023, 5:06 p.m.