[R] Error: missing value where TRUE/FALSE needed

Fox, John jfox at mcmaster.ca
Thu Jun 9 15:53:47 CEST 2016


Dear Shashi Seth,

The principal problem here is that the "argument" to if() must be logical, while comparing anything to NA always produces NA. The proper way to test for NA is with is.na(). 

There's potentially something more subtle going on here, however, which is that even if some of the elements of the logical expression in the "call" to if() are NA, the expression may evaluate to FALSE (but never to TRUE) if the non-NA elements imply that it is FALSE. Consider the following examples:

> TRUE && FALSE && NA  # FALSE regardless of the value of the last element
[1] FALSE

> TRUE && TRUE && NA  # truth depends on the value of the last element
[1] NA

> (TRUE && FALSE && NA) != NA
[1] NA

> (TRUE && TRUE && NA) != NA
[1] NA

> is.na(TRUE && FALSE && NA)
[1] FALSE

> is.na(TRUE && TRUE && NA)
[1] TRUE

I hope that this helps,
 John

-----------------------------
John Fox, Professor
McMaster University
Hamilton, Ontario
Canada L8S 4M4
Web: socserv.mcmaster.ca/jfox



> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of SHASHI SETH
> Sent: June 9, 2016 2:53 AM
> To: R-help at r-project.org
> Subject: [R] Error: missing value where TRUE/FALSE needed
> 
> Hi,
> 
> 
> 
> I am getting the following error:
> 
> Error in if ((sum > 0 && sums1 > 0 && sums2 > 0) != NA) { :
> 
> missing value where TRUE/FALSE needed
> 
> 
> 
> 
> 
> I have including my code below for your review:
> 
> 
> 
> fitness_1_data
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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