10

Creating Dummy Variables Using R

 3 years ago
source link: https://www.excelr.com/blog/data-science/creating-dummy-variables-using-r
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Creating Dummy Variables Using R
31Aug

Creating Dummy Variables Using R

233

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)


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK