How to Convert Factors to Numeric in R


How to Convert Factors to Numeric in R ?

Answer

To convert factors to numeric values in R, you need to first convert the factor to a character vector and then to a numeric vector. This ensures that the factor levels are correctly transformed into their numeric equivalents.



✐ Examples

1 Converting a Factor Representing Age Groups to Numeric Values

In this example,

  1. We start by creating a character vector named age_groups which contains the values 'Young', 'Middle', 'Old', and 'Young'. This vector represents different age groups.
  2. Next, we use the factor() function to convert the age_groups vector into a factor. We assign the result to a variable named age_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then convert the age_factor to a numeric vector using the as.numeric() function directly on the factor. This step converts the factor levels to their corresponding numeric codes.
  4. We assign the result to a variable named age_numeric.
  5. We print the age_numeric vector to the console to see the numeric representation of the age groups. This allows us to verify the conversion.

R Program

age_groups <- c('Young', 'Middle', 'Old', 'Young')
age_factor <- factor(age_groups)
age_numeric <- as.numeric(age_factor)
print(age_numeric)

Output

[1] 3 2 1 3

2 Converting a Factor Representing Ratings to Numeric Values

In this example,

  1. We start by creating a character vector named ratings which contains the values 'Low', 'Medium', 'High', 'Medium', and 'Low'. This vector represents different rating levels.
  2. Next, we use the factor() function to convert the ratings vector into a factor. We assign the result to a variable named ratings_factor. The factor() function automatically identifies the unique levels of the vector.
  3. We then convert the ratings_factor to a numeric vector using the as.numeric() function directly on the factor. This step converts the factor levels to their corresponding numeric codes.
  4. We assign the result to a variable named ratings_numeric.
  5. We print the ratings_numeric vector to the console to see the numeric representation of the ratings. This allows us to verify the conversion.

R Program

ratings <- c('Low', 'Medium', 'High', 'Medium', 'Low')
ratings_factor <- factor(ratings)
ratings_numeric <- as.numeric(ratings_factor)
print(ratings_numeric)

Output

[1] 2 3 1 3 2

3 Converting a Factor Representing Satisfaction Levels to Numeric Values Using Levels

In this example,

  1. We start by creating a character vector named satisfaction which contains the values 'Unsatisfied', 'Neutral', 'Satisfied', and 'Very Satisfied'. This vector represents different levels of customer satisfaction.
  2. Next, we use the factor() function to convert the satisfaction vector into a factor. We assign the result to a variable named satisfaction_factor. The factor() function automatically identifies the unique levels of the vector.
  3. To convert the factor to numeric values directly based on the factor levels, we use the as.numeric() function on the factor itself. This assigns numeric values to each level based on their order in the factor.
  4. We assign the result to a variable named satisfaction_numeric.
  5. We print the satisfaction_numeric vector to the console to see the numeric representation of the satisfaction levels. This allows us to verify the conversion.

R Program

satisfaction <- c('Unsatisfied', 'Neutral', 'Satisfied', 'Very Satisfied')
satisfaction_factor <- factor(satisfaction, levels = c('Unsatisfied', 'Neutral', 'Satisfied', 'Very Satisfied'))
satisfaction_numeric <- as.numeric(satisfaction_factor)
print(satisfaction_numeric)

Output

[1] 1 2 3 4

Summary

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