# 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.

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 #  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 2 1 3 2 2 # attr(,"levels") #  "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``` 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``` 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:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

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

• 