Label Encoding
The Label encoding method is for encoding categorical variables that assigns the number value to each distinct value. For the instance, the numerical values 1, 2, and 3 might be assigned to a categorical variable with the three unique values of “red,” “green,” and “blue,” respectively.
The factor() function in R can be used to turn a category variable into a factor, that can subsequently be turned into integers using the as.integer() function.
Consider the following data frame as an illustration:
R
color <- c ( "red" , "green" , "blue" , "blue" , "red" ) df <- data.frame (color) |
We can use the following code to label encrypt the color column :
R
df$color <- as.integer ( factor (df$color)) |
Output:
It should be noted that the numbers given to each unique value are chosen at random and have no inherent significance.