Use "NA" for logical operation on R

Asked

Viewed 138 times

2

I’m with 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.

2 answers

5


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

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

2

Complementing:

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

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

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

Other software does not have this idiosyncrasy. On the other hand, the other way of doing it, assigning the numerium value (whether very large or very small) to Missing causes potential problems in some logic tests (Ex: in Stata v>0, would return not only the positive values but also the values of v)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.