Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical operators (AND, OR) with NA, TRUE and FALSE

Tags:

I cannot understand the properties of logical (boolean) values TRUE, FALSE and NA when used with logical OR (|) and logical AND (&). Here are some examples:

NA | TRUE # [1] TRUE  NA | FALSE # [1] NA  NA & TRUE # [1] NA  NA & FALSE # [1] FALSE 

Can you explain these outputs?

like image 405
Remi.b Avatar asked May 30 '13 19:05

Remi.b


People also ask

Is a logical operators True or false?

Logical (Boolean) Operators. Logical expressions, like comparison expressions, return a true (1) or false (0) value when processed. Logical operators combine two comparisons and return the true (1) or false (0) value depending on the results of the comparisons.

Is && True or false?

If applied to boolean values, the && operator only returns true when both of its operands are true (and false in all other cases), while the || operator only returns false when both of its operands are false (and true in all other cases).

What are the 5 logical operators?

There are five logical operator symbols: tilde, dot, wedge, horseshoe, and triple bar.


1 Answers

To quote from ?Logic:

NA is a valid logical object. Where a component of x or y is NA, the result will be NA if the outcome is ambiguous. In other words NA & TRUE evaluates to NA, but NA & FALSE evaluates to FALSE. See the examples below.

The key there is the word "ambiguous". NA represents something that is "unknown". So NA & TRUE could be either true or false, but we don't know. Whereas NA & FALSE will be false no matter what the missing value is.

like image 184
joran Avatar answered Oct 14 '22 05:10

joran