Use "NA" in logical operation in R

2

I have the situation:

a = c(1,2,3, NA, 5, 6, NA)
b = 99
for(i in 1: 7){
  if(a[i] == "NA"){
    a[i] = b
  }
}

The problem is that R does not make the logical comparison using "NA" or NA.

    
asked by anonymous 22.09.2014 / 19:42

2 answers

5

Actually when doing [i] == "NA", you are testing if [i] is equal to string 'NA'. To make the comparison, you should use the is.na (a [i]) function.

a = c(1,2,3, NA, 5, 6, NA) 
b = 99 
for(i in 1: 7)
{ 
    if(is.na(a[i]))
    {
        a[i] = b 
    } 
}
    
22.09.2014 / 19:47
2

Complementing:

In other statistical packages, the míssing value is a number. In SAS, for example, it's a very small number, so Stata is a very large number. Then it is possible to make a test comparing the numbers of a vector with the missing, which in the background is another number.

In R, this is not possible, because NA is not a number, nor a string, it is a specific type, different from given. Therefore it makes no sense to compare if a vector v == NA.

That's why you have to use the function. is.na ().

In other software you do not have this idiosyncracy. On the other hand, the other way of doing, assigning numeric value (either too large or too small) to missing causes potential problems in some logic tests (eg in Stata v> 0, would return not only the positive values but the missing values of v )

    
22.09.2014 / 20:09