samedi 31 décembre 2016

Why is the second ifelse not evaluated?

In the code below, I do not understand why the second ifelse() is not evaluated. Can someone explain it? How to rewrite the ifelse statement?

The y1==1 and y2==1 in the last row are matched, and the column result should be filled with 1.

df <- data.frame(x1=c(0,0,NA,NA,NA),y1=c(NA,NA,NA,NA,1),x2=c(0,NA,NA,0,NA),
                 y2=c(1,NA,NA,NA,1))

df$result <- with(df, ifelse((x1==0 & x2==0), 0, ifelse((y1==1 & y2==1), 1, 100)))

Aucun commentaire:

Enregistrer un commentaire