# Convert Categorical Variable to Numeric in R

**Methods – finnstats**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Convert Categorical Variable to Numeric in R, In this tutorial, you’ll learn how to convert categorical values into quantitative values to make statistical modeling easier.

Most statistical models can’t take in strings as inputs. We’ll go through the conversion based on the airline data set that we reviewed in the previous post.

The “Reporting Airline” feature in the Airline dataset is a categorical variable with nine character types: “AA”, “AS”, “B6”, “DL”, “HP”, “PA (1)”, “TW”, “UA” or “VX”.

You’ll need to convert these variables into a numeric format for further investigation.

## Convert Categorical Variable to Numeric in R

Let’s load the data set into the R console.

library(tidyverse) library(dplyr) library(ggplot2) data<-read.csv("D:/RStudio/Airlinedata.csv",1) head(data)

To fix this difficulty, add additional features matching to each unique element in the original feature you want to encode the data.

Because the feature “Reporting Airline” includes nine values, you need to construct nine new features, such as “AA,” “AS,” “B6,” and so on.

When a value appears in the original feature, you set the new feature’s matching value to one; the remainder of the features are set to zero.

In the first row of the reporting airline example, the reporting airline is “UA”. As a result, the feature “UA” would be set to one, while the other features would be set to zero.

Similarly, the reporting airline value for the second row is “AS.” As a result, we set “AS” to one and all other features to zero.

### Spread() Method

To convert category variables to dummy variables in tidyverse, use the spread() method.

data %>% mutate(dummy=1) %>% spread(key=Reporting_Airline,value=dummy, fill=0) %>% slice(1:5)

To do so, use the spread() function with three arguments:

key, which is the column to convert into categorical values, in this case, “Reporting Airline”;

value, which is the value you want to set the key to (in this case “dummy”);

and fill, which fills the missing values with zero if they are otherwise left as NAs.

Alternatively, you can assign flight delay, “ArrDelay,” values to each feature instead of the dummy numbers 0 or 1.

In the first row of the reporting airline example, the reporting airline is “UA,” and the arrival delay is 2 minutes. As a result, you can set the feature “UA” to 2 and the remaining features to NA.

Similarly, the reported airline value for the second row is “AS,” and the arrival delay is -21 minutes. you can set the feature “AS” to -21 and the remaining features to NA.

You’ll also utilize the spread() method to accomplish this. Based on the previous example, the key argument can be set to the “Reporting Airline” column.

The value argument can be either 0 or 1, or the values in the “ArrDelay” column can be assigned.

data %>% spread(key=Reporting_Airline,value=ArrDelay)

Because no parameter fill is declared in this example, the function will return NA for the missing data.

Remember that if you utilize the “Reporting Airline” and “ArrDelay” columns as inputs in the spread() method, the “Reporting Airline” and “ArrDelay” columns will be removed from the output by default.

You learned a strategy for converting category values to numeric values in this tutorial. That means you learned “one-hot encoding”.

How is it possible?

Because this method is known as “one-hot encoding”.

The post Convert Categorical Variable to Numeric in R appeared first on finnstats.

**leave a comment**for the author, please follow the link and comment on their blog:

**Methods – finnstats**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.