2

Since "is.na(NA)" returns true and "NA > 0" returns NA, "is.na(NA) & (NA > 0)" should return NA which is true.

is.na(NA) & NA >0

[1] NA

In the same way, "!is.na(NA)" returns false and "NA > 0" returns NA, "!is.na(NA) & (NA > 0)" should return NA as well. But R returns false. Why is this?

!is.na(NA) & NA >0

[1] FALSE

Community
  • 1
  • 1

1 Answers1

2

According to ?"&"

NA is a valid logical object. Where a component of x or y is NA, the result will be NA if the outcome is ambiguous. In other words NA & TRUE evaluates to NA, but NA & FALSE evaluates to FALSE. See the examples below.

In the OP's condition, the first one evaluates to

TRUE & NA #(is.na(NA)#[1] TRUE;NA > 0#[1] NA)

and second is

FALSE & NA #(!is.na(NA)#[1] FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • When I use `?&`, it returns the following error: `Error: unexpected '&' in "?&"`. How were you able to get the explanation? – user3119750 Dec 18 '19 at 16:38
  • @user3119750 You can either quote or use backquotes. I used backquotes, but when I copy/pasted, it got removed – akrun Dec 18 '19 at 16:40