Skip to content

Instantly share code, notes, and snippets.

@jonesor
Last active July 18, 2026 20:21
Show Gist options
  • Select an option

  • Save jonesor/9226171af14873ad332d to your computer and use it in GitHub Desktop.

Select an option

Save jonesor/9226171af14873ad332d to your computer and use it in GitHub Desktop.
Remove unused factor levels in a data frame
# Remove unused factor levels in a data frame.
# Base R's droplevels() does exactly this (drops unused levels from every factor
# column) and is the recommended replacement for the original hand-rolled loop,
# which errored on non-factor columns.
dropall <- function(x){
droplevels(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment