Recode, collapse, and order factor levels using a single function with regex matching
问题 I find manipulating factor variables in R unduly complicated. Things I frequently want to do when cleaning factors include: Resorting levels – not just to set a reference category, but also put all levels in a logical (non-alphabetical order) for summary tables. x <- factor(x, levels = new.order) Recode / rename factor levels – to simplify names and/or collapse multiple categories into one group. For one-to-one recoding levels(x) <- new.levels(x) or plyr::revalue , see here or here for