Skip to content
Prev 322934 / 398502 Next

Why can't R understand if(num!=NA)?

An NA value means that the value is unknown.  E.g.,
  age <- NA
means the you do not know the age of your subject.
(The subject has an age, NA means you did not collect
that data.)  Thus you do not know the value of 
  age == 6
either, the subject might be 6 or it might not be.
Hence R makes the value of age==6  NA.

Since R does not have different evaluation rules for literal values
and expressions that means that NA==6 and NA==someAge
must evaluate to NA as well.

The second part of the question is why
   if (NA) { } else { }
causes an error.  It is a bit arbitrary, but there is a mismatch
between a 2-way 'if' statement and 3-valued logical data
and R deals with it by insisting that the condition in
   if (condition) { } else {}
be either TRUE or FALSE, not NA.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com