Jose Jimenez
Jose Jimenez

Reputation: 1

Changing a categorical variable to binary 0 and 1

so what I am trying to do is make the values in the variable default, from the Default dataset, Yes = 1, and No = 0. I plot it to check if it works, but what I have been doing does not work, as it gives me 1 and 2 for yes and no. Thank you in advance for any help! Working in RStudio.r

library(ISLR)
Default
Default$default=as.numeric(Default$default, levels=c("Yes","No"),labels=c(1,0))
model= lm(default~balance, data=Default)
with(Default,plot(balance,default))

Upvotes: 0

Views: 3874

Answers (2)

George
George

Reputation: 903

Is this what you are after

Default$default <- ifelse(Default$default == "Yes",1,0)
model= lm(default~balance, data=Default)
with(Default,plot(balance,default))

Upvotes: 1

Hong Ooi
Hong Ooi

Reputation: 57686

Try this:

Default$default <- as.numeric(Default$default == "Yes")

Upvotes: 0

Related Questions