One-hot encoding in r
# 0. sample data
# 1. mltools, data.table
# 2. reshape2
# 0. sample data
set.seed(42)
data <- data.frame(
color = seq(1, 50,by=1),
variable = sample(c("R","G","B"), 50, replace = TRUE)
)
head(data)
# 1. method1: mltools, data.table
library(mltools)
library(data.table)
# if not is.factor, change as.factor
# data <- data %<% mutate_if(is.character, as.factor)
(newdata_method1 <- one_hot(as.data.table(data)))
# 2. reshape2
library(reshape2)
(newdata_method2 <- dcast(data=data, color ~ variable, length))
ref : https://datatricks.co.uk/one-hot-encoding-in-r-three-simple-methods