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

William Dunlap wdunlap at tibco.com
Fri May 3 18:14:26 CEST 2013

> if(num!=NA)
> Why doesn't the first statement work?

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

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of jpm miao
> Sent: Friday, May 03, 2013 8:25 AM
> To: r-help
> Subject: [R] Why can't R understand if(num!=NA)?
> I have a program, when I write
> if(num!=NA)
> it yields an error message.
> However, if I write
> if(is.na(num)==FALSE)
> it works.
> Why doesn't the first statement work?
> Thanks,
> Miao
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list