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
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
0 comments: