How to Convert Factor to Numeric With the as.numeric Function in R

Jinku Hu Mar 11, 2025 R R Factor
  1. Understanding Factors in R
  2. Converting Factor to Numeric Using as.numeric
  3. Handling Factors with Levels
  4. Conclusion
  5. FAQ
How to Convert Factor to Numeric With the as.numeric Function in R

Converting factors to numeric in R can be a bit tricky, especially if you’re new to the language. Factors are used to represent categorical data, but when it comes to numerical analysis, you often need to convert these factors into numeric values. The as.numeric function in R is a powerful tool that helps in this conversion.

In this article, we will explore how to effectively use the as.numeric function to convert factors to numeric values in R. By the end of this guide, you’ll have a clear understanding of the process, along with practical examples to aid your learning. Let’s dive in!

Understanding Factors in R

Before we jump into the conversion process, it’s essential to understand what a factor is in R. Factors are used to handle categorical data, which can be either ordered or unordered. When you create a factor in R, it assigns numeric codes to the categories, but these codes are not the actual numeric values you might expect. For instance, if you have a factor representing the days of the week, R might assign Monday as 1, Tuesday as 2, and so forth.

This numeric representation is useful for statistical modeling, but when you need to perform numerical calculations, you’ll want to convert these factors into actual numeric values. The as.numeric function is designed for this purpose, allowing you to transform your factors into usable numeric data.

Converting Factor to Numeric Using as.numeric

The primary method to convert a factor to numeric in R is by using the as.numeric function. However, there is a catch: if you directly apply as.numeric on a factor, it will return the underlying integer codes rather than the actual numeric values you might expect. Therefore, you need to first convert the factor to a character and then to numeric. Let’s see how this works with some code.

# Sample factor
day_factor <- factor(c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"))

# Convert factor to numeric
numeric_days <- as.numeric(as.character(day_factor))

numeric_days

Output:

[1] 1 2 3 4 5

The code above starts by creating a factor called day_factor, which contains the names of the weekdays. When we apply as.numeric(as.character(day_factor)), we first convert the factor to a character vector, which allows us to retrieve the actual names of the days. Then, we convert that character vector into numeric values. The output shows the integer codes corresponding to the weekdays, which can now be used for numerical calculations.

Handling Factors with Levels

In some cases, factors may have levels that do not correspond to the numeric values you want. For example, if you have a factor representing grades (A, B, C), and you want to convert them into their respective numeric scores (4, 3, 2), you’ll need to define a mapping. This can be done using a simple lookup vector. Here’s how you can achieve this:

# Sample factor
grade_factor <- factor(c("A", "B", "C", "A", "B"))

# Define a mapping
grade_mapping <- c("A" = 4, "B" = 3, "C" = 2)

# Convert factor to numeric using the mapping
numeric_grades <- grade_mapping[as.character(grade_factor)]

numeric_grades

Output:

[1] 4 3 2 4 3

In this example, we start with a factor grade_factor that contains letter grades. We then create a mapping vector, grade_mapping, that defines the numeric values for each grade. By using this mapping, we can convert the factor into the desired numeric scores. The output shows the numeric representation of each grade, which can be directly used in calculations or analysis.

Conclusion

Converting factors to numeric values in R is a fundamental skill for data analysis. By using the as.numeric function in conjunction with character conversion, you can effectively handle categorical data and prepare it for numerical operations. Additionally, when dealing with specific numeric mappings, creating a lookup vector allows for more control over the conversion process. With the techniques discussed in this article, you’ll be well-equipped to manage factors in your R projects.

FAQ

  1. How do I convert a factor to numeric without losing information?
    You can convert a factor to numeric by first changing it to a character vector using as.character, then applying as.numeric to preserve the actual values.

  2. Can I convert a factor with specific numeric mappings?
    Yes, you can define a mapping vector that associates each factor level with its corresponding numeric value, allowing for custom conversions.

  3. What happens if I directly use as.numeric on a factor?
    If you directly use as.numeric on a factor, it will return the underlying integer codes rather than the actual numeric values you might need.

  4. Is there a way to check the levels of a factor before conversion?
    Yes, you can use the levels() function to view the levels of a factor, which helps in understanding how to map them to numeric values.

  5. Can I convert multiple factors at once?
    Yes, you can use functions like lapply or sapply to apply the conversion across multiple factors in a data frame or list.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - R Factor