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]]
& -> 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 <ramnik.bansal at gmail.com>:> 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]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
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] NAWhat 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
> 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}}