# [R] [FORGED] Logical Operators' inconsistent Behavior

Ramnik Bansal ramnik.bansal at gmail.com
Sat May 20 05:22:55 CEST 2017

```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'.

But this doesn't seem to be true because if that was the case it
should have worked with character operands as well, albeit following
the rules of NA in logical operations ( as per R help manual)

For e.g.
> x <- "abc"; c(as.logical(x) & F, as.logical(x) & T, as.logical(x) | F, as.logical(x) | T)
[1] FALSE    NA    NA  TRUE

Whereas
> x <- "abc"; c(x & F, x & T, x | F, x | T)
Error in x & F :
operations are possible only for numeric, logical or complex types

So my question basically is :
What does R actually do in the case of complex numbers Vs characters
as operands in logical operations ?

consistent with above behavior with character,
> NA_character_ & FALSE
Error in NA_character_ & FALSE :
operations are possible only for numeric, logical or complex types

R documentation on the other hand mentions:
Logical computations treat NA as a missing TRUE/FALSE value and so may
return TRUE or FALSE if the expression does not depend on the NA
operand.

Isn't NA mentioned in the R documentation to be interpreted as NA
irrespective of the type? Else, shouldn't the R documentation mention
"Logical computations treat NA except NA_character_ as a missing..."

Thanks,
Ramnik

ps: I hope I have phrased my question well enough this time not to end
up inviting the wrath of some R-Gods around on a mere R-mortal that I
am at this stage :) The more am becoming familiar with R, the more am
loving it and definitely don't intend to offend R Gods!

On Fri, May 19, 2017 at 6:57 PM, Ted Harding <ted.harding at wlandres.net> wrote:
> Also, I havve had an additional thought which may clarify things
> for R users].
> is FALSE, provided logical NA is either TRUE ot FALSE but,
> because the "NA" says that it is not known which it is,
> it could be "anything". And, indeed, if "NA" is given the
> "missing" meaning and if we assume that a missing logical value
> did indeed have a value (necessarily either TRUE or FALSE),
> then it follows logically that FALSE & NA = FALS£.
>
> On the other hand, if with the "missing" interpretation of "NA"
> we don't even know that it is a logical, then it might be fair
> enough to say FALSE & NA = NA.
> Ted.
>
> Testing to see what would happen if the NA were not loigical,
> I put myself (not being logical ... ) on the line, facing up to R:
>    X <- "Ted"
>    FALSE & X
>    Error in FALSE & X :
>    operations are possible only for numeric, logical or complex types
> So R will refuse to deal with any variable which cannot partake in
> a logical expression.
>
> Ted.
>
> On Fri, 2017-05-19 at 14:24 +0200, Jérémie Juste wrote:
>> My apologies if I was not clear enough,
>>
>> TRUE & NA could be either TRUE or FALSE and consequently is NA.
>> why is   FALSE & NA = FALSE?  NA could be TRUE or FALSE, so FALSE & NA
>> should be NA?
>>
>>
>> On Fri, May 19, 2017 at 2:13 PM, Rolf Turner <r.turner at auckland.ac.nz>
>> wrote:
>>
>> > On 20/05/17 00:01, Jérémie Juste wrote:
>> >
>> >> Hello,
>> >>
>> >> Rolf said,
>> >>
>> >> 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.
>> >>
>> >>
>> >> According to this logic why is
>> >>
>> >>     FALSE & NA
>> >>
>> >> [1] FALSE
>> >>
>> >
>> > Huh????
>> >
>> >
>> > cheers,
>> >
>> > Rolf Turner
>> >
>> > --
>> > Technical Editor ANZJS
>> > Department of Statistics
>> > University of Auckland
>> > Phone: +64-9-373-7599 ext. 88276
>> >
>>
>>
>>
>> --
>> Jérémie Juste
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

```