0

I am a beginner in R programming. In the dataset that I want to work, there are 3 categorical variables that I want to convert into (0's and 1's).

This is how I tried to do it, but it didn't change anything?

A = mydata$industry
A[which(A=="yes")] = 1
A[which(A=="no")] = 0 

Thanks in advance for the help.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
Melanie
  • 3
  • 2

2 Answers2

1

Try this:

mydata$industry <- ifelse(mydata$industry=="yes", 1, 0)
Vincent
  • 15,809
  • 7
  • 37
  • 39
0

Maybe you can try the code below with dummy data of A

A <- c("no", "yes", "no", "no", "yes")

such that

> as.integer(factor(A, labels = c("no", "yes"))) - 1
[1] 0 1 0 0 1
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81