[R] A note on == versus logical operators for logical comparisons

Bert Gunter bgunter.4567 at gmail.com
Wed Oct 5 20:12:23 CEST 2016


This post was suggested by a recent post of Fabien Verger. In it, he used
a != b
as an exclusive-or operator for two logical vectors, a and b. As R
already has an exlcusive-or operator, I suggested
which is implemented as
(x | y) & !(x & y)
i.e. through purely logical operators.


> b <- sample(c(TRUE,FALSE),1e6,rep=TRUE)
> a <- sample(c(TRUE,FALSE),1e6,rep=TRUE)

> system.time(as.logical(b) != as.logical(a))
   user  system elapsed
  0.002   0.000   0.002

> system.time(xor(a,b))
   user  system elapsed
  0.022   0.002   0.023

I added the as.logical() cast to handle the case, e.g. a = 1 and b = 2
, for which xor() gives FALSE and a != b gives TRUE without the cast.
Such mixups can occur.

So given the must greater efficiency of != (or ==) I wondered why that
wasn't used for xor(). Here's why:

> xor("A","B")
Error in x | y :
  operations are possible only for numeric, logical or complex types

> as.logical("A") != as.logical("B")
[1] NA

That is, xor()'s behavior as a logical operator is consistent with the
other logical operators (of course) , while the != implementation is

There's a moral or two here maybe (beside: "Bert's not the brightest
bulb in the chandelier"):

1) There is almost always a good reason why R's core implementation
does things the way it does, at least at the user level (I of course
cannot comment on the underlying C code). If you think there's a
better way, it's probably worthwhile to think again.

2) == is actually a pretty blunt instrument for vector comparisons
(which Fabien explicitly noted in his post in the context of numeric
comparisons); unless you have pretty strong control over what's being
compared, other perhaps less efficient but more robust methods should
be considered.

Just my view of course.


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

More information about the R-help mailing list