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
  • 104,352
  • 13
  • 194
  • 196
Melanie
  • 3
  • 2

2 Answers2

1

Try this:

mydata$industry <- ifelse(mydata$industry=="yes", 1, 0)
Vincent
  • 12,931
  • 7
  • 35
  • 34
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
  • 80,151
  • 7
  • 17
  • 65