> 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}}
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 <S.Ellison at lgcgroup.com> 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}}
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
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] FALSEHuh???? cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
> > 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 > SQLIt'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}}
Hadley Wickham
2017-May-21 14:00 UTC
[R] [FORGED] Logical Operators' inconsistent Behavior
On Fri, May 19, 2017 at 6:38 AM, S Ellison <S.Ellison at lgcgroup.com> 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'.That's not TRUE ;) sqlite> select (3 > 2) OR NULL; 1 sqlite> select (4 < 3) AND NULL; 0 Hadley -- http://hadley.nz
William Michels
2017-May-22 04:35 UTC
[R] [FORGED] Logical Operators' inconsistent Behavior
Looking below and online, R's truth tables for NOT, AND, OR are identical to the NOT, AND, OR truth tables originating from Stephen Cole Kleene's "strong logic of indeterminacy", as demonstrated on the Wikipedia page entitled, "Three-Valued Logic"--specifically in the section entitled "Kleene and Priest Logics": https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics> > ttNOT <- cbind(c(FALSE, NA, TRUE), !c(FALSE, NA, TRUE)) > rownames(ttNOT) <- c("False", "na", "True") > colnames(ttNOT) <- c("A", "Not(A)") > ttNOTA Not(A) False FALSE TRUE na NA NA True TRUE FALSE> > ttAND <- outer(c(FALSE, NA, TRUE), c(FALSE, NA, TRUE), "&" ) > rownames(ttAND) <- c("False", "na", "True") > colnames(ttAND) <- c("False", "na", "True") > ttANDFalse na True False FALSE FALSE FALSE na FALSE NA NA True FALSE NA TRUE> > ttOR <- outer(c(FALSE, NA, TRUE), c(FALSE, NA, TRUE), "|" ) > rownames(ttOR) <- c("False", "na", "True") > colnames(ttOR) <- c("False", "na", "True") > ttORFalse na True False FALSE NA TRUE na NA NA TRUE True TRUE TRUE TRUE> >The bottom section of the same Wikipedia page (section entitled "Application in SQL" ), and an additional Wikipedia page entitled "Null (SQL)" discusses how the Kleene logic described above is differentially implemented in SQL. https://en.wikipedia.org/wiki/Null_(SQL) HTH, Bill William Michels, Ph.D. On Sun, May 21, 2017 at 7:00 AM, Hadley Wickham <h.wickham at gmail.com> wrote:> On Fri, May 19, 2017 at 6:38 AM, S Ellison <S.Ellison at lgcgroup.com> 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'. > > That's not TRUE ;) > > sqlite> select (3 > 2) OR NULL; > 1 > > sqlite> select (4 < 3) AND NULL; > 0 > > Hadley > > > -- > http://hadley.nz > > ______________________________________________ > R-help at r-project.org 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.
William Michels
2017-May-22 18:04 UTC
[R] [FORGED] Logical Operators' inconsistent Behavior
Evaluation of the NOT, AND, OR logical statements below in MySQL 5.5.30-log Community Server (GPL) replicate R's truth tables for NOT, AND, OR. See MySQL queries (below), which are in agreement with R truth table code posted in this thread: bash-3.2$ mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 346 Server version: 5.5.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT FALSE, NULL, TRUE; +-------+------+------+ | FALSE | NULL | TRUE | +-------+------+------+ | 0 | NULL | 1 | +-------+------+------+ 1 row in set (0.00 sec) mysql> SELECT NOT FALSE, NOT NULL, NOT TRUE; +-----------+----------+----------+ | NOT FALSE | NOT NULL | NOT TRUE | +-----------+----------+----------+ | 1 | NULL | 0 | +-----------+----------+----------+ 1 row in set (0.00 sec) mysql> SELECT FALSE AND FALSE, -> FALSE AND NULL, -> FALSE AND TRUE; +-----------------+----------------+----------------+ | FALSE AND FALSE | FALSE AND NULL | FALSE AND TRUE | +-----------------+----------------+----------------+ | 0 | 0 | 0 | +-----------------+----------------+----------------+ 1 row in set (0.00 sec) mysql> SELECT NULL AND NULL, -> NULL AND TRUE, -> TRUE AND TRUE; +---------------+---------------+---------------+ | NULL AND NULL | NULL AND TRUE | TRUE AND TRUE | +---------------+---------------+---------------+ | NULL | NULL | 1 | +---------------+---------------+---------------+ 1 row in set (0.00 sec) mysql> SELECT TRUE OR TRUE, -> NULL OR TRUE, -> FALSE OR TRUE; +--------------+--------------+---------------+ | TRUE OR TRUE | NULL OR TRUE | FALSE OR TRUE | +--------------+--------------+---------------+ | 1 | 1 | 1 | +--------------+--------------+---------------+ 1 row in set (0.00 sec) mysql> SELECT NULL OR NULL, -> FALSE OR NULL, -> FALSE OR FALSE; +--------------+---------------+----------------+ | NULL OR NULL | FALSE OR NULL | FALSE OR FALSE | +--------------+---------------+----------------+ | NULL | NULL | 0 | +--------------+---------------+----------------+ 1 row in set (0.00 sec) mysql> HTH, Bill William Michels, Ph.D. On Sun, May 21, 2017 at 7:00 AM, Hadley Wickham <h.wickham at gmail.com> wrote:> On Fri, May 19, 2017 at 6:38 AM, S Ellison <S.Ellison at lgcgroup.com> 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'. > > That's not TRUE ;) > > sqlite> select (3 > 2) OR NULL; > 1 > > sqlite> select (4 < 3) AND NULL; > 0 > > Hadley > > > -- > http://hadley.nz > > ______________________________________________ > R-help at r-project.org 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.