mardi 7 août 2018

if function to create a new variable from three dummies in R

I need R to create a new categorical variable using three dummies that I have in my dataset. I am trying to use "if" and "else if". I have managed to do it in other sotfware but I can't find the solution in R. Would someone please take a look at my code and tell me what's wrong?

    data$newvariable<- if(data$dummyA[1]) {
   (data$newvariable<-"1")
    } else if(data$dummyB[1]){
    (data$newvariable<-"2")
    } else if(data$dummyC[1]){
    (data$newvariable<-"3") }

I hope not to duplicate questions. Sorry! And thank you in advance!

What I get as a result is all cases categorized as "1", which makes me think there is something missing between the first if statement and the following one. But I can't find the solution, help, please!

THANKS!

Aucun commentaire:

Enregistrer un commentaire