Clean up and Reorder Factors/Categories in R

Another reminder of my classics. Sometimes, when you reduce data in a data frame some levels of categories are no longer contained in the observations after filtering but the factors still contain them in their levels.

To clean these up use the following command:

cleanfactor <- as.factor(as.character(oldfactor))

That’s it again. Cheers,
iss