How to Reorder Factor Levels in R


How to Reorder Factor Levels in R ?

Answer

To reorder factor levels in R, you can use the factor() function with the levels argument to specify the desired order of levels. This is useful when you need to change the order in which levels appear in a factor.



✐ Examples

1 Reordering Levels of a Factor Representing Days of the Week

In this example,

  1. We start by creating a character vector named days which contains the values 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', and 'Sunday'. This vector represents days of the week in order.
  2. Next, we use the factor() function to convert the days vector into a factor. We assign the result to a variable named days_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then use the factor() function again to reorder the levels of the days_factor by specifying the levels in a new order: c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'). This new order starts the week from Sunday.
  4. We print the modified days_factor to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.

R Program

days <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
days_factor <- factor(days)
days_factor <- factor(days_factor, levels = c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'))
print(days_factor)

Output

[1] Monday    Tuesday   Wednesday Thursday  Friday    Saturday  Sunday   
Levels: Sunday Monday Tuesday Wednesday Thursday Friday Saturday

2 Reordering Levels of a Factor Representing Size Categories

In this example,

  1. We start by creating a character vector named sizes which contains the values 'small', 'medium', 'large', and 'extra large'. This vector represents different size categories.
  2. Next, we use the factor() function to convert the sizes vector into a factor. We assign the result to a variable named sizes_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then use the factor() function again to reorder the levels of the sizes_factor by specifying the levels in a new order: c('extra large', 'large', 'medium', 'small'). This new order represents a descending size order.
  4. We print the modified sizes_factor to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.

R Program

sizes <- c('small', 'medium', 'large', 'extra large')
sizes_factor <- factor(sizes)
sizes_factor <- factor(sizes_factor, levels = c('extra large', 'large', 'medium', 'small'))
print(sizes_factor)

Output

[1] small      medium     large      extra large
Levels: extra large large medium small

3 Reordering Levels of a Factor Representing Fruit Categories

In this example,

  1. We start by creating a character vector named fruits which contains the values 'apple', 'banana', 'cherry', and 'date'. This vector represents different types of fruits.
  2. Next, we use the factor() function to convert the fruits vector into a factor. We assign the result to a variable named fruits_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then use the factor() function again to reorder the levels of the fruits_factor by specifying the levels in a new order: c('date', 'cherry', 'banana', 'apple'). This new order represents the fruits in alphabetical order starting from the end.
  4. We print the modified fruits_factor to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.

R Program

fruits <- c('apple', 'banana', 'cherry', 'date')
fruits_factor <- factor(fruits)
fruits_factor <- factor(fruits_factor, levels = c('date', 'cherry', 'banana', 'apple'))
print(fruits_factor)

Output

[1] apple  banana cherry date 
Levels: date cherry banana apple

Summary

In this tutorial, we learned How to Reorder Factor Levels in R language with well detailed examples.




More R Factors Tutorials

  1. How to Create Factors in R ?
  2. How to find Length of a Factor in R ?
  3. How to Loop over a Factor in R ?
  4. How to Convert Data to Factors in R ?
  5. How to Order Factor Levels in R ?
  6. How to Access Factor Levels in R ?
  7. How to Modify Factor Levels in R ?
  8. How to Reorder Factor Levels in R ?
  9. How to Add Levels to a Factor in R ?
  10. How to Drop Levels from a Factor in R ?
  11. How to Rename Levels of a Factor in R ?
  12. How to Use Factors in Data Frames in R ?
  13. How to Generate Summary Statistics for Factors in R ?
  14. How to Merge Factors in R ?
  15. How to Split Data by Factors in R ?
  16. How to Plot Factors in R ?
  17. How to Convert Factors to Numeric in R ?
  18. How to Convert Factors to Character in R ?
  19. How to Handle Missing Values in Factors in R ?
  20. How to Use Factors in Conditional Statements in R ?
  21. How to Compare Factors in R ?
  22. How to Create Ordered Factors in R ?
  23. How to Check if a Variable is a Factor in R ?
  24. How to Use Factors in Statistical Models in R ?
  25. How to Collapse Factor Levels in R ?
  26. How to Use Factors in Grouping Operations in R ?
  27. How to Use Factors in Aggregation Functions in R ?
  28. How to Deal with Unused Factor Levels in R ?
  29. How to Encode and Decode Factors in R ?
  30. How to Use Factors in Regression Analysis in R ?
  31. How to Convert Factors to Dates in R ?