Suppose I have a column in a data frame as colors say c(\"Red\", \"Blue\", \"Blue\", \"Orange\")
.
I would like to get it as c(1,2,2,3)
.
<
Here is a function based on previous code:
# Recode 'string' into 'integer'
recode_str_int <- function(df, feature) {
# 1. Unique values
# 1.1. 'string' values
list_str <- sort(unique(df[, feature]))
# 1.2. 'integer' values
list_int <- 1:length(list_str)
# 2. Create new feature
# 2.1. Names
names(list_int) = list_str
df$feature_new = list_int[df[, feature]]
# 3. Result
df$feature_new
} # recode_str_int
Call it like:
df$new_feature <- recode_str_int(df, "feature")