How to Modify Factor Levels in R


How to Modify Factor Levels in R ?

Answer

To modify factor levels in R, you can use the levels() function to set new levels. This is useful when you need to rename, reorder, or add new levels to a factor.



✐ Examples

1 Renaming Factor Levels of a Character Factor

In this example,

  1. We start by creating a character vector named colors which contains the values 'red', 'green', 'blue', and 'red'. This vector represents categorical data.
  2. Next, we use the factor() function to convert the colors vector into a factor. We assign the result to a variable named colors_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then use the levels() function to access the levels of the colors_factor. We assign new names to the levels by setting levels(colors_factor) to a new vector of level names c('Red', 'Green', 'Blue').
  4. We print the modified colors_factor to the console to see the renamed factor levels. This allows us to verify the changes in the factor levels.

R Program

colors <- c('red', 'green', 'blue', 'red')
colors_factor <- factor(colors)
levels(colors_factor) <- c('Red', 'Green', 'Blue')
print(colors_factor)

Output

[1] Red   Green Blue  Red  
Levels: Red Green Blue

2 Reordering Factor Levels

In this example,

  1. We start by creating a character vector named animals which contains the values 'cat', 'dog', 'bird', and 'cat'. This vector represents categorical data.
  2. Next, we use the factor() function to convert the animals vector into a factor. We assign the result to a variable named animals_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 animals_factor by specifying the levels in the desired order: c('dog', 'cat', 'bird').
  4. We print the modified animals_factor to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.

R Program

animals <- c('cat', 'dog', 'bird', 'cat')
animals_factor <- factor(animals)
animals_factor <- factor(animals_factor, levels = c('dog', 'cat', 'bird'))
print(animals_factor)

Output

[1] cat  dog  bird cat 
Levels: dog cat bird

3 Adding New Factor Levels

In this example,

  1. We start by creating a character vector named sizes which contains the values 'small', 'medium', and 'large'. This vector represents categorical data.
  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 levels() function to add new levels to the sizes_factor by setting levels(sizes_factor) to a new vector of level names c('small', 'medium', 'large', 'extra large').
  4. We print the modified sizes_factor to the console to see the added factor levels. This allows us to verify the new levels in the factor.

R Program

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

Output

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

Summary

In this tutorial, we learned How to Modify 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 ?