Given a group list and a factor, this helper function collapses the levels in the factor accordingly and also ensures that the resulting levels are in the order given in the group list.
Examples
set.seed(123)
x <- factor(sample(
c("ASIAN", "BLACK OR AFRICAN AMERICAN", "MULTIPLE", "UNKNOWN", "WHITE"),
size = 30L,
replace = TRUE
))
group_list <- list(
"ASIAN/BLACK OR AFRICAN AMERICAN" = c("ASIAN", "BLACK OR AFRICAN AMERICAN"),
"MULTIPLE/UNKNOWN" = c("MULTIPLE", "UNKNOWN"),
"WHITE" = "WHITE"
)
x_collapsed <- h_collapse_levels(x, group_list)
stopifnot(identical(levels(x_collapsed), names(group_list)))