powered by
Collapse several values in a categorical variable into one level
collapseLevels( .data, var, levels, collapse = paste(levels, collapse = "_"), name = sprintf("%s.coll", var) )
the original dataframe containing a new column of the collapsed variable with tidyverse code attached
a dataframe to collapse
a character of the name of the categorical variable to collapse
a character vector of the levels to be collapsed
name of the newly created level
a name for the new variable
Owen Jin
code
collapsed <- collapseLevels(iris, var = "Species", levels = c("setosa", "virginica")) cat(code(collapsed)) head(collapsed)
Run the code above in your browser using DataLab