2

Why is this so in R?

> F & NA
[1] FALSE
> T & NA
[1] NA

I would expect the first line of the code to evaluate to NA as well. People have told me 'this is simply strange behavior of R', but is there some other notion to it?

tomka
  • 2,516
  • 7
  • 31
  • 45
  • 5
    Because when you do `F & condition2`, R doesn't check `condition2`, relying on the rule `F & something` is always `F`. Though I'm not sure because `F & ("a"+1)` throws an error (`F && ("a"+1)` does not). – Stéphane Laurent Feb 12 '19 at 16:42
  • 5
    If the first value in an AND is false, then it doesn't matter what the second value is, the answer is going to be false. But if the first value is true, then the AND will only be true if the second value is also true, but since it's missing (NA), it's impossible to say whether the expression will be true or false. – MrFlick Feb 12 '19 at 16:43
  • @MrFlick I've just edited my previous comment. I added that however, `F & ("a"+1)` throws an error (I expected `F`). So R "looks" at `condition2`. – Stéphane Laurent Feb 12 '19 at 16:45
  • 1
    To add to what is said above, `TRUE && something` is always `something`. – Rui Barradas Feb 12 '19 at 16:46
  • 1
    @StéphaneLaurent That's because with `&` the expression is still evaluated but it's not with `&&` because that's "short circuited." So it's not necessarily that `&` ignores the second parameter, it just has enough information to know with certainty what the result is. `NA & F` also returns FALSE. – MrFlick Feb 12 '19 at 16:47
  • It's strange that `F & "apple"` craps out, but `F && "apple"` works. They **should** produce the same result (and that result should be the same as `F && NA`). – eddi Feb 12 '19 at 17:47

1 Answers1

6

If you have an AND (&) statement and one of the values is false, then it doesn't matter what the other value is, the answer is going to be false. The NA value means that a value is missing, but the unobserved value must be a true or false and either way you're going to get false back.

But if one of the values is true, then the AND will only be true if the second value is also true. However in this case the missing value (NA), could be true or false so it's impossible to say whether the expression will be. Thus R has to propagate the NA value.

MrFlick
  • 195,160
  • 17
  • 277
  • 295