[R] [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner r.turner at auckland.ac.nz
Fri May 19 14:12:58 CEST 2017

On 19/05/17 23:38, S Ellison wrote:
>> TRUE & FALSE is FALSE but TRUE & TRUE is TRUE, so TRUE & NA could be
>> either TRUE or FALSE and consequently is NA.
>> OTOH FALSE & (anything) is FALSE so FALSE & NA is FALSE.
>> As I said *think* about it; don't just go with your immediate knee-jerk
>> (simplistic) reaction.
> Hmm... not sure that was quite fair to the OP.

The OP complained that the logical operators in R are inconsistent. 
This is an arrogant and presumptuous assertion that deserves a 
reprimand.  One should be very, very circumspect about presuming to know 
better than R.

> Yes, FALSE &
> <anything>== FALSE. But 'NA' does not mean 'anything'; it means 'missing' (see
> ?'NA').

Well, duh.  Yes, I know what NA means.  If it's missing, you don't know 
what it's value is.  But it doesn't *matter* what its value is; FALSE &
<anything> is FALSE.  So FALSE & NA is FALSE, irrespective of what NA
"really" is.

> It is much less obvious that FALSE & <missing> should generate a
> non-missing value. SQL, for example, generally takes the view that any
> expression involving 'missing' is 'missing'.

Well, then SQL gets it wrong.

> And R's behaviour can look odd if the vagaries of real data intervene:
> b1 <- c(A=TRUE, C=FALSE)
> b2 <- c(A=FALSE, B=FALSE, C=TRUE)
> b1['B'] & b2['B']
> #Which returns
> # <NA>
> which - particularly since it appears without warning ....

Everything appears without warning.  Nobody expected the Spanish 

> .... - is not an obviously sensible outcome.

Why not?  It's obviously sensible to me, and to anyone else who is 
thinking.  Since b1['B'] is NA (b1 doesn't have an entry named 'B') and 
b2['B'] is FALSE we get NA & FALSE which is FALSE.  Where's the problem?

The "<NA>" in the output that you show is the *name* of the 'B' entry of 
b1, and since there isn't one, it doesn't have a name.  So the name is 
missing whence it is rendered as "<NA>" (missing character).

> I am not suggesting a change to R's logical operations, which have
> clearly been thought through (that is evident from NA&FALSE ==
> FALSE&NA == FALSE). But R's behaviour looks to me like a choice among
> difficult alternatives, rather than the only possible choice. I'd
> give the OP some credit for that.

You seem to be arguing for the sake of arguing.  It's really quite 
straightforward *if* you *think* about it.



Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

More information about the R-help mailing list