lundi 24 mai 2021

How to use NA in if..else if statement in R

I created this small example. I want to print some values, for example, B for NA values using the if else statement.

x = c(1,7,NA, 3, NA, NA)

for(i in 1: length(x)){
  y = x[i]
  if(y == 1){
    print("A")
  }
  else if(y == 'NA'){
    print("B")
  }
  else{
    print("C")
  }
}

I am getting an error message Error in if (y == 1) { : missing value where TRUE/FALSE needed Why can't I print B for NA values? How to use NA within the if else statement?

Aucun commentaire:

Enregistrer un commentaire