lundi 17 juin 2019

R ifelse statement with many loops

x=data.frame("l"=c('a','b','d','a','c','c','d'),
             "t"=c(1:7))
x$l1 = ifelse(x$l=="a",1,
              ifelse(x$l=="b",2,
                     ifelse(x$l=="c",3,
                            ifelse(x$l=="d" ,4,
                                   ifelse(x$l=="d" & x$t==7,5,NA)))))

As you can see on the final statement, if l == "d" and t == 7 then l1 should equals to 5 but it gets set equals to 4...

Aucun commentaire:

Enregistrer un commentaire