powered by
Collapse values in a categorical variable into one defined level
collapse_cat(data, var, levels, new_level, name = NULL)
the original dataframe containing a new column of the collapsed variable with tidyverse code attached
a dataframe to collapse
a string of the name of the categorical variable to collapse
a character vector of the levels to be collapsed
a string for the new level
a name for the new variable
Zhaoming Su
code
collapsed <- collapse_cat(iris, var = "Species", c("versicolor", "virginica"), new_level = "V" ) cat(code(collapsed)) tail(collapsed)
Run the code above in your browser using DataLab