How to Convert Factors to Numeric in R - Step by Step Examples
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,
- 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. - Next, we use the
factor()
function to convert theage_groups
vector into a factor. We assign the result to a variable namedage_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - We then convert the
age_factor
to a numeric vector using theas.numeric()
function directly on the factor. This step converts the factor levels to their corresponding numeric codes. - We assign the result to a variable named
age_numeric
. - 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,
- 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. - Next, we use the
factor()
function to convert theratings
vector into a factor. We assign the result to a variable namedratings_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - We then convert the
ratings_factor
to a numeric vector using theas.numeric()
function directly on the factor. This step converts the factor levels to their corresponding numeric codes. - We assign the result to a variable named
ratings_numeric
. - 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,
- 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. - Next, we use the
factor()
function to convert thesatisfaction
vector into a factor. We assign the result to a variable namedsatisfaction_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - 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. - We assign the result to a variable named
satisfaction_numeric
. - 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
- How to Create Factors in R ?
- How to find Length of a Factor in R ?
- How to Loop over a Factor in R ?
- How to Convert Data to Factors in R ?
- How to Order Factor Levels in R ?
- How to Access Factor Levels in R ?
- How to Modify Factor Levels in R ?
- How to Reorder Factor Levels in R ?
- How to Add Levels to a Factor in R ?
- How to Drop Levels from a Factor in R ?
- How to Rename Levels of a Factor in R ?
- How to Use Factors in Data Frames in R ?
- How to Generate Summary Statistics for Factors in R ?
- How to Merge Factors in R ?
- How to Split Data by Factors in R ?
- How to Plot Factors in R ?
- How to Convert Factors to Numeric in R ?
- How to Convert Factors to Character in R ?
- How to Handle Missing Values in Factors in R ?
- How to Use Factors in Conditional Statements in R ?
- How to Compare Factors in R ?
- How to Create Ordered Factors in R ?
- How to Check if a Variable is a Factor in R ?
- How to Use Factors in Statistical Models in R ?
- How to Collapse Factor Levels in R ?
- How to Use Factors in Grouping Operations in R ?
- How to Use Factors in Aggregation Functions in R ?
- How to Deal with Unused Factor Levels in R ?
- How to Encode and Decode Factors in R ?
- How to Use Factors in Regression Analysis in R ?
- How to Convert Factors to Dates in R ?