[R] [FORGED] Logical Operators' inconsistent Behavior

S Ellison S.Ellison at LGCGroup.com
Fri May 19 13:38:06 CEST 2017


> 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. Yes,  FALSE & <anything> == FALSE. But 'NA' does not mean 'anything'; it means 'missing' (see ?'NA'). 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'. 

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> 
# FALSE 

which - particularly since it appears without warning - is not an obviously sensible outcome. 

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.

S Ellison


S Ellison







*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list