Creating Dummy Variables Using R
Creating Dummy Variables Using R

Creating Dummy Variables Using R

1997

Below Is The Code For Creating Dummy Variables

data("iris")example <-as.data.frame(c("setosa","versicolor","virginica"))
names(example) <-"species"
#For every unique value in the string coilumn, 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 (examples$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)

 

Post Comments

Call Us