본문 바로가기
R

[R] one hot encoding in r

by Chandler.j 2021. 10. 5.
반응형

fig 1. title

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)

fig 2. output of 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)))

fig 3. output of newdata_method1


# 2. reshape2

library(reshape2)

(newdata_method2 <- dcast(data=data, color ~ variable, length))

fig 4. output of newdata_method2

 

ref : https://datatricks.co.uk/one-hot-encoding-in-r-three-simple-methods

 

One-hot encoding in R: three simple methods - Data Tricks

One-hot encoding is an important step in training any machine learning algorith. Here are three simple methods for performing one-hot encoding in R with examples.

datatricks.co.uk

 


TOP

Designed by 티스토리