I created some sample data below to help illustrate my question.
library(dplyr)
col1 = paste(rep('var',5),seq(1:5), sep = "")
Value = c(1,1,0,NA,NA)
p1 <- data.frame(col1,Value)
> p1
col1 Value
var1 1
var2 1
var3 0
var4 NA
var5 NA
When is.na(Value) is placed first in the ifelse statement, mutate works as expected.
> p1 %>% mutate(NewCol = ifelse(is.na(Value), "TestYes",
ifelse(Value == 1, "Test1Yes",
ifelse(Value == 0, "Test0Yes","No"))))
col1 Value NewCol
var1 1 Test1Yes
var2 1 Test1Yes
var3 0 Test0Yes
var4 NA TestYes
var5 NA TestYes
When I place is.na(Value) as the second ifelse statement, it doesnt work. But the third ifelse statement still works checking for Value == 0. The second ifelse statement with is.na(Value) is skipped over.
> p1 %>% mutate(NewCol = ifelse(Value == 1, "Test1Yes",
ifelse(is.na(Value), "TestYes",
ifelse(Value == 0, "Test0Yes","No"))))
col1 Value NewCol
var1 1 Test1Yes
var2 1 Test1Yes
var3 0 Test0Yes
var4 NA <NA>
var5 NA <NA>
Am I missing something in the code or is there a reason why is.na needs to be placed first in the ifelse statements?