mercredi 15 août 2018

Issues with more than one "&" condition inside if statement in R

I'm doing an IF statement in R to create a variable in R.

I'm having an error that I can't detect to what it refers exactly so I can't fix it. Can somebody help me?

library(install.load)
install_load("checkmate", "expss")

amostra$escol <- NA
educd003 <- data.frame("d003" = 1:9, "codeduc" = c(1,1,3,3,5,5,7,9,9))
educd0091 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
                        "codeduc" = c(2,2,4,4,4,4,6,6,6,8,10,10))
educd0092 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
                        "codeduc" = c(1,1,3,3,3,3,5,5,5,7,9,9))

for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
  amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
  amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
  amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
  amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
  amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
  amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
  amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
  amostra$escol[i] <- NA
}
}

Error:

Error in if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] ==  : 
  missing value where TRUE/FALSE needed

Thanks, Wagner

Aucun commentaire:

Enregistrer un commentaire