虽然之前在这里讨论过opacity
和NAs
,但我想了解这个问题的特定细节,而这个细节之前还没有解决。
可复制的示例:
ifelse
为什么transflag <- c("YES", "YES", "NO", "NO", "NO", "NO", "YES", "YES", "NO")
class <- c("SC", "IC", "IC", "SC", NA, "IC", "IC", NA, NA)
prod <- c("P1", "P2", NA, "P1", "P2", "P1", NA, "P2", "P1")
data <- data.frame(class, prod, transflag)
data$transflag <- as.character(data$transflag )
data$class <- as.character(data$class )
data$prod <- as.character(data$prod )
data$transflag4 <- ifelse(data$prod == "P1" & data$class == "SC" , "NO", data$transflag)
data$transflag5 <- ifelse(data$prod == "P2" & data$class == "SC" , "NO", data$transflag)
中的结果是NA,而定义了transflag4
中的结果呢?考虑到其他发布者提到transflag5
对任何事物的评估为NA
(recode variables with NAs with ifelse() in R),这很奇怪