Re-computing the levels of factor

Problem

You want to do re-compute the levels of a factor. This is useful when a factor contains levels that aren’t actually present in the data. This can happen during data import, or when you remove some rows.

Solution

For a single factor object:

  1. # Create a factor with an extra level (gamma)
  2. x <- factor(c("alpha","beta","alpha"), levels=c("alpha","beta","gamma"))
  3. x
  4. #> [1] alpha beta alpha
  5. #> Levels: alpha beta gamma
  6. # Remove the extra level
  7. x <- factor(x)
  8. x
  9. #> [1] alpha beta alpha
  10. #> Levels: alpha beta

After importing data, you may have a data frame with a mix of factors and other kinds vectors, and want to re-compute the levels of all the factors. You can use the droplevels() function to do this.

  1. # Create a data frame with some factors (with extra levels)
  2. df <- data.frame(
  3. x = factor(c("alpha","beta","alpha"), levels=c("alpha","beta","gamma")),
  4. y = c(5,8,2),
  5. z = factor(c("red","green","green"), levels=c("red","green","blue"))
  6. )
  7. # Display the factors (with extra levels)
  8. df$x
  9. #> [1] alpha beta alpha
  10. #> Levels: alpha beta gamma
  11. df$z
  12. #> [1] red green green
  13. #> Levels: red green blue
  14. # Drop the extra levels
  15. df <- droplevels(df)
  16. # Show the factors again, now without extra levels
  17. df$x
  18. #> [1] alpha beta alpha
  19. #> Levels: alpha beta
  20. df$z
  21. #> [1] red green green
  22. #> Levels: red green

See also

To re-compute the levels of all the factor columns in a data frame, see ../Re-computing_the_levels_of_all_factor_columns_in_a_data_frame.