Convert Categorical Variable to Numeric in R (2 Examples)

 

This post shows how to convert a categorical variable to numeric in the R programming language.

Table of contents:

It’s time to dive into the examples…

 

Example 1: Convert Categorical Vector Object to Numeric

Example 1 illustrates how to convert a categorical vector to numeric in R.

For this, we first have to create an example vector:

x <- factor(c("cat_a", "cat_b", "cat_a",    # Create categorical vector
              "cat_c", "cat_b", "cat_b"))
x                                           # Print categorical vector
# [1] cat_a cat_b cat_a cat_c cat_b cat_b
# Levels: cat_a cat_b cat_c

As you can see based on the previous output of the RStudio console, we have created a vector object consisting of three different categories.

Note that this data object has the factor class. In case your data has the character class, you have to convert this character to the factor class first.

Next, we can use the unclass function to set the categories of our vector to numeric:

x_new <- unclass(x)                         # Convert categories to numeric
x_new                                       # Print updated vector
# [1] 1 2 1 3 2 2
# attr(,"levels")
# [1] "cat_a" "cat_b" "cat_c"

The previous output shows our converted vector, i.e. a numeric (or integer) data object with six elements and three different values that correspond to the factor levels of our input vector.

 

Example 2: Convert Categorical Data Frame Columns to Numeric

In this example, I’ll illustrate how to convert all categorical variables of a data frame to numeric.

First, we have to create some example data:

data <- data.frame(x1 = letters[1:6],       # Create data frame
                   x2 = LETTERS[5:4],
                   x3 = "x",
                   stringsAsFactors = TRUE)
data                                        # Print data frame

 

table 1 data frame convert categorical variable numeric r

 

After running the previous R programming syntax the data frame you can see in Table 1 has been created.

Now, we can use the sapply and unclass functions to convert multiple variables to numeric in one line of R code:

data_new <- sapply(data, unclass)           # Convert categorical variables
data_new                                    # Print updated data frame

 

table 2 matrix convert categorical variable numeric r

 

In Table 2 you can see that we have created a new data matrix that contains numeric columns instead of factorial columns.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, I show the R programming codes of this tutorial:

 

 

In addition to the video, you could read the other tutorials of my website. I have released several related articles already.

 

This post has demonstrated how to change categorical vectors and data frame columns to the numeric class in the R programming language. In case you have any further comments and/or questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top