Categorical Data Encoding in R

Introduction to Categorical Data

Hello, Space Voyager! Today, we're venturing through fascinating territory: Categorical Data Encoding! Categorical data consists of groups or traits such as gender, marital status, or hometown. We convert categories into numbers using Label and One-Hot Encoding techniques to assist our machine-learning counterparts.

Concept of Label Encoding

Label Encoding maps categories to numbers ranging from 0 through N-1, where N represents the count of unique categories. It's beneficial for ordered data, such as Small, Medium, and Large.

In R, we can achieve this with the help of the factor function. Let's illustrate this with a vector of shirt sizes:

R
sizes <- c("Small", "Medium", "Large")

sizes_factor <- factor(sizes, levels = c("Small", "Medium", "Large"), labels = c(0, 1, 2))
print(sizes_factor)
# Output:
# [1] 0 1 2
# Levels: 0 1 2

Here, [1] 0 1 2 represents the new numerical values assigned to each size respectively, indicating the encoded values of Small, Medium, and Large as 0, 1, and 2. Levels: 0 1 2 denotes the possible unique values that the factor levels can take after encoding.

To encode a column of categorical data in a data frame, consider the following example:

R
df <- data.frame(gender = c("Male", "Female", "Female", "Male"))

df$gender_factor <- factor(df$gender, levels = c("Male", "Female"), labels = c(1, 2))
print(df)
# Output:
#    gender gender_factor
# 1    Male             1
# 2  Female             2
# 3  Female             2
# 4    Male             1

In this example, we encode the gender column, assigning 1 to Male and 2 to Female.

Concept of One-Hot Encoding

One-Hot Encoding creates additional columns for each category, placing a 1 in the appropriate category and zeros (0) everywhere else. It's preferred for nominal data, where no order is relevant, such as Red, Green, Blue.

The model.matrix function facilitates achieving one-hot encoding in R. This function creates a matrix from a data frame based on a given formula and is useful for one-hot encoding. Key arguments:

  • formula: ~ variable - 1, where variable is the categorical column and - 1 removes the intercept.
  • data: The data frame containing the specified variable. Here is an example:
R
colors <- c("Red", "Green", "Blue", "Red", "Green")

df <- data.frame(colors)

df_onehot <- model.matrix(~colors-1, df)
print(df_onehot)
# Output:
#   colorsBlue colorsGreen colorsRed
# 1          0           0         1
# 2          0           1         0
# 3          1           0         0
# 4          0           0         1
# 5          0           1         0

For a more complex data frame, consider:

R
df <- data.frame(color = c("Red", "Blue"), size = c("Small", "Large"))

# Dummy coding for 'color'
df_color_onehot <- model.matrix(~color-1, df)

# Binding the one-hot encoded color back to the original data
df_final <- cbind(df, df_color_onehot)
print(df_final)
# Output:
#   color  size colorBlue colorRed
# 1   Red Small        0       1
# 2  Blue Large        1       0

This demonstrates encoding within a data frame that includes multiple columns.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal