Learn R Programming

iNZightTools (version 1.13.0)

collapseLevels: Collapse data by values of a categorical variable

Description

Collapse several values in a categorical variable into one level

Usage

collapseLevels(
  .data,
  var,
  levels,
  collapse = paste(levels, collapse = "_"),
  name = sprintf("%s.coll", var)
)

Value

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

Arguments

.data

a dataframe to collapse

var

a character of the name of the categorical variable to collapse

levels

a character vector of the levels to be collapsed

collapse

name of the newly created level

name

a name for the new variable

Author

Owen Jin

See Also

code

Examples

Run this code
collapsed <- collapseLevels(iris, var = "Species",
    levels = c("setosa", "virginica"))
cat(code(collapsed))
head(collapsed)

Run the code above in your browser using DataLab