Cretaing Dummy Varialbes Using R

Cretaing Dummy Varialbes Using R

Create dummy variables for a categorical data using “R” to Normalize it with 1/0:

data(“iris”)
example <- as.data.frame(c(“setosa”,”versicolor”,”virginica”))
names(example) <- “Species”
#For every unique value in the string column, create a new 1/0 column
#This is what Factors do “under-the-hood” automatically when passed to function requiring numeric data
for(level in unique(example$Species)){
example[paste(“dummy”, level, sep = “_”)] <- ifelse(example$Species == level, 1, 0)
}
View (names(example))
Mat <- as.matrix(example)
View(Mat)
irismerged <- merge(iris,Mat)
View(irismerged)

To know more Please Register 

ExcelR is one of the leading training providers of professional certification training solutions in the world. We strive to provide the best training methods, taught by industry professionals at locations all over the world. Partner with us today and see why we are the number one choice for business professionals all over the world.

0 comments: