Logical Operators' inconsistent Behavior

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Logical Operators' inconsistent Behavior

ramnikbansal
Hi,

I need to understand the inconsistent behaviour of & and I operators when
used with NA.

The code below explains this inconsistency

> TRUE & NA
[1] NA

> FALSE & NA
[1] FALSE

> TRUE & NA
[1] NA

> FALSE | NA
[1] NA

> TRUE | NA
[1] TRUE

> TRUE == NA
[1] NA

> FALSE == NA
[1] NA

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Logical Operators' inconsistent Behavior

Thierry Onkelinx
& -> AND -> results only TRUE if both inputs are TRUE. Hence: FALSE AND
unknown = FALSE, TRUE AND unknown = unknown
| -> OR -> results in TRUE as soon as one of the inputs is TRUE. Hence FASE
or unknown = unknown, TRUE or unknown = TRUE
TRUE == NA and FALSE == NA compares TRUE/FALSE against unknown hence the
output is unknown.

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2017-05-19 11:48 GMT+02:00 Ramnik Bansal <[hidden email]>:

> Hi,
>
> I need to understand the inconsistent behaviour of & and I operators when
> used with NA.
>
> The code below explains this inconsistency
>
> > TRUE & NA
> [1] NA
>
> > FALSE & NA
> [1] FALSE
>
> > TRUE & NA
> [1] NA
>
> > FALSE | NA
> [1] NA
>
> > TRUE | NA
> [1] TRUE
>
> > TRUE == NA
> [1] NA
>
> > FALSE == NA
> [1] NA
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner
In reply to this post by ramnikbansal

On 19/05/17 21:48, Ramnik Bansal wrote:

> Hi,
>
> I need to understand the inconsistent behaviour of & and I operators when
> used with NA.
>
> The code below explains this inconsistency
>
>> TRUE & NA
> [1] NA
>
>> FALSE & NA
> [1] FALSE
>
>> TRUE & NA
> [1] NA
>
>> FALSE | NA
> [1] NA
>
>> TRUE | NA
> [1] TRUE
>
>> TRUE == NA
> [1] NA
>
>> FALSE == NA
> [1] NA

What inconsistency?  It all makes complete sense.  Think about it.

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.

Und so weiter.

As I said *think* about it; don't just go with your immediate knee-jerk
(simplistic) reaction.

cheers,

Rolf Turner

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

S Ellison-2
> 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}}

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Jeremie Juste
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
?

Best regards,

Jeremie

On Fri, May 19, 2017 at 1:38 PM, S Ellison <[hidden email]> 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. 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 u...{{dropped:18}}

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner
In reply to this post by S Ellison-2
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>
> # FALSE
>
> which - particularly since it appears without warning ....

Everything appears without warning.  Nobody expected the Spanish
Inquisition.

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

cheers,

Rolf

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner
In reply to this post by Jeremie Juste
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Jeremie Juste
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 <[hidden email]>
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

S Ellison-2
In reply to this post by Rolf Turner
> > SQL, for example, generally takes the view that any
> > expression involving 'missing' is 'missing'.
>
> Well, then SQL gets it wrong.

Well, that's a view. But paraphrasing an  R Turner from a few lines away in the same email:

> One should be very, very circumspect about presuming to know better than
> SQL

It's a choice. I understand and respect R's. But I can also understand why someone might have expected something different.

S


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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Peter Dalgaard-2
In reply to this post by Jeremie Juste

> On 19 May 2017, at 14:24 , Jérémie Juste <[hidden email]> 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?
>

At the risk of flogging a dead horse:

FALSE & TRUE = FALSE
FALSE & FALSE = FALSE

FALSE & x = FALSE, whatever the value of x, hence

FALSE & NA = FALSE

Get it?  

-pd


>
> On Fri, May 19, 2017 at 2:13 PM, Rolf Turner <[hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Duncan Murdoch-2
In reply to this post by S Ellison-2
On 19/05/2017 8:48 AM, S Ellison wrote:

>>> SQL, for example, generally takes the view that any
>>> expression involving 'missing' is 'missing'.
>>
>> Well, then SQL gets it wrong.
>
> Well, that's a view. But paraphrasing an  R Turner from a few lines away in the same email:
>
>> One should be very, very circumspect about presuming to know better than
>> SQL
>
> It's a choice. I understand and respect R's. But I can also understand why someone might have expected something different.

You're right about SQL.  But for R, it's pretty simple to read the help
page on NA, and it is quite explicit about this:

"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."

I'm surprised nobody on this thread has quoted that before.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Jeff Newmiller
In reply to this post by Jeremie Juste
FALSE & FALSE -> FALSE
FALSE & TRUE -> FALSE

Why do you need to know what the second value is? It doesn't matter what it is... the answer is FALSE.
--
Sent from my phone. Please excuse my brevity.

On May 19, 2017 5:24:06 AM PDT, "Jérémie Juste" <[hidden email]> 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 <[hidden email]>
>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]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Ted Harding
In reply to this post by Jeremie Juste
[I unadvertently sent my reply below to Jeremie, instead of R-help.
Also, I havve had an additional thought which may clarify things
for R users].
[Original reply]:
The point about this is that (as Rolf wrote) FALSE & (anything)
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.

[Additional thought]:
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 <[hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

ramnikbansal
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 ?

And adding some more:

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 <[hidden email]> wrote:

> [I unadvertently sent my reply below to Jeremie, instead of R-help.
> Also, I havve had an additional thought which may clarify things
> for R users].
> [Original reply]:
> The point about this is that (as Rolf wrote) FALSE & (anything)
> 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.
>
> [Additional thought]:
> 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 <[hidden email]>
>> 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]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Martin Maechler
>>>>> Ramnik Bansal <[hidden email]>
>>>>>     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).

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Logical Operators' inconsistent Behavior

Duncan Murdoch-2
On 20/05/2017 5:53 AM, Martin Maechler wrote:

>>>>>> Ramnik Bansal <[hidden email]>
>>>>>>     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).
>

The surprise is that as.logical("TRUE") returns TRUE, whereas automatic
coercion doesn't apply to character strings.  I don't think we should
change this, but it is an inconsistency.  (We could perhaps mention it
in the ?logical help page.)

Duncan Murdoch

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner
On 20/05/17 22:18, Duncan Murdoch wrote:

> On 20/05/2017 5:53 AM, Martin Maechler wrote:
>>>>>>> Ramnik Bansal <[hidden email]>
>>>>>>>     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).
>>
>
> The surprise is that as.logical("TRUE") returns TRUE, whereas automatic
> coercion doesn't apply to character strings.  I don't think we should
> change this, but it is an inconsistency.  (We could perhaps mention it
> in the ?logical help page.)


Actually it *is* mentioned.  From ?logical:

> Character strings c("T", "TRUE", "True", "true") are regarded as
> true,  c("F", "FALSE", "False", "false") as false, and all others as NA.

cheers,

Rolf

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: [FORGED] Logical Operators' inconsistent Behavior

Duncan Murdoch-2
On 20/05/2017 6:39 AM, Rolf Turner wrote:

> On 20/05/17 22:18, Duncan Murdoch wrote:
>> On 20/05/2017 5:53 AM, Martin Maechler wrote:
>>>>>>>> Ramnik Bansal <[hidden email]>
>>>>>>>>     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).
>>>
>>
>> The surprise is that as.logical("TRUE") returns TRUE, whereas automatic
>> coercion doesn't apply to character strings.  I don't think we should
>> change this, but it is an inconsistency.  (We could perhaps mention it
>> in the ?logical help page.)
>
>
> Actually it *is* mentioned.  From ?logical:
>
>> Character strings c("T", "TRUE", "True", "true") are regarded as
>> true,  c("F", "FALSE", "False", "false") as false, and all others as NA.
>

I meant that the negative part should be mentioned:  this only works
with an explicit as.logical(), not with implicit coercion.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: [FORGED] Logical Operators' inconsistent Behavior

Rolf Turner
On 20/05/17 22:42, Duncan Murdoch wrote:

> On 20/05/2017 6:39 AM, Rolf Turner wrote:
>> On 20/05/17 22:18, Duncan Murdoch wrote:
>>> On 20/05/2017 5:53 AM, Martin Maechler wrote:
>>>>>>>>> Ramnik Bansal <[hidden email]>
>>>>>>>>>     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).
>>>>
>>>
>>> The surprise is that as.logical("TRUE") returns TRUE, whereas automatic
>>> coercion doesn't apply to character strings.  I don't think we should
>>> change this, but it is an inconsistency.  (We could perhaps mention it
>>> in the ?logical help page.)
>>
>>
>> Actually it *is* mentioned.  From ?logical:
>>
>>> Character strings c("T", "TRUE", "True", "true") are regarded as
>>> true,  c("F", "FALSE", "False", "false") as false, and all others as NA.
>>
>
> I meant that the negative part should be mentioned:  this only works
> with an explicit as.logical(), not with implicit coercion.

Ah, I see.  I missed the point.

cheers,

Rolf

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: [FORGED] Logical Operators' inconsistent Behavior

ramnikbansal
In the case of logical operations performed with an operand being a
character type, the error is:
"operations are possible only for numeric, logical or complex types"

Now the display of this error message seems to be the OUTCOME of the
fact that implicit coercion is NOT BEING APPLIED INTERNALLY to
character operands in the case of logical operations but is applied to
all other "numeric" type operands. This error message is NOT the
CAUSE.

Well, why not:

F & "abc" return a FALSE because if F & NA can return  FALSE, the
logic being that once an operand is FALSE in case of AND operation
then the output should be False, IRRESPECTIVE of the second operand.
In that case how does it matter if the second operand  is "abc" or
4+5i or NA? The output is deterministically FALSE anyways.

Effectively the point that I want to make is: Treat the character
operand as NA ( except "F", "FALSE", "T", "TRUE" etc which can be
treated as FALSE/TRUE) and then apply the logical operations. Or in
short "Apply implicit coercion on character types as well in case of
logical operations."




On Sat, May 20, 2017 at 4:15 PM, Rolf Turner <[hidden email]> wrote:

> On 20/05/17 22:42, Duncan Murdoch wrote:
>>
>> On 20/05/2017 6:39 AM, Rolf Turner wrote:
>>>
>>> On 20/05/17 22:18, Duncan Murdoch wrote:
>>>>
>>>> On 20/05/2017 5:53 AM, Martin Maechler wrote:
>>>>>>>>>>
>>>>>>>>>> Ramnik Bansal <[hidden email]>
>>>>>>>>>>     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).
>>>>>
>>>>
>>>> The surprise is that as.logical("TRUE") returns TRUE, whereas automatic
>>>> coercion doesn't apply to character strings.  I don't think we should
>>>> change this, but it is an inconsistency.  (We could perhaps mention it
>>>> in the ?logical help page.)
>>>
>>>
>>>
>>> Actually it *is* mentioned.  From ?logical:
>>>
>>>> Character strings c("T", "TRUE", "True", "true") are regarded as
>>>> true,  c("F", "FALSE", "False", "false") as false, and all others as NA.
>>>
>>>
>>
>> I meant that the negative part should be mentioned:  this only works
>> with an explicit as.logical(), not with implicit coercion.
>
>
> Ah, I see.  I missed the point.
>
>
> cheers,
>
> Rolf
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
12