15.11 Removing Unused Levels from a Factor
15.11.2 Solution
Sometimes, after processing your data you will have a factor that contains levels that are no longer used. Here’s an example:
factor(c("small", "large", "large", "small", "medium"))
sizes <- sizes[1:3]
sizes <-
sizes#> [1] small large large
#> Levels: large medium small
To remove them, use droplevels()
:
droplevels(sizes)
#> [1] small large large
#> Levels: large small