[R] [FORGED] Logical Operators' inconsistent Behavior

Martin Maechler maechler at stat.math.ethz.ch
Sat May 20 11:53:26 CEST 2017


>>>>> Ramnik Bansal <ramnik.bansal at gmail.com>
>>>>>     on Sat, 20 May 2017 08:52:55 +0530 writes:

    > Taking this question further.
    > If I use a complex number or a numeric as an operand in logical
    > operations, to me it APPEARS that these two types are first coerced to
    > LOGICAL internally and then THIS logical output is further used as the
    > operand.

    > For eg.
    >> x <- 4+5i; c(x & F, x & T, x | F, x | T)
    > [1] FALSE  TRUE  TRUE  TRUE

    > This output is consistent with
    >> x <- 4+5i; c(as.logical(x) & F, as.logical(x) & T, as.logical(x) | F, as.logical(x) | T)
    > [1] FALSE  TRUE  TRUE  TRUE

    > This consistency makes me draw an on-the-surface conclusion that in
    > the case of logical operations if the operand is not of type 'logical'
    > it is first coerced into 'logical'.

That conclusion is wrong as you show below.
Rather, as the error message says,
logical
	"operations are possible only for numeric, logical or complex types"

Again:

1) Logical/Arithmetic  operations "work" with "numeric-like" types, namely
  numeric, logical or complex, (and numeric = {integer, double})

  ==> all other types give an error (the one you've cited twice)

2) For "numeric-like" types and *logical* operations (&, |, !; plus && and ||)
   the equivalent of as.logical() is applied before performing the Op.
   
Seems pretty consistent ...
and also according to the principle of "least surprise" (for me at least).



More information about the R-help mailing list