0

Say we create a vector like this:

v <- c(1, 2, 2, NA)

And we test it for a simple condition:

> v == 1
[1]  TRUE FALSE FALSE    NA

Subsetting the vector using this condition, we obtain this:

> v[v == 1]
[1]  1 NA

Why is the NA included in the subset when it clearly doesn't meet the condition? I'm aware that NA == 1 returns NA, but why is the subsetting not taking TRUE values exclusively? What am I missing here?

Zweifler
  • 375
  • 1
  • 4
  • 12
  • See [this answer](https://stackoverflow.com/a/49890440/12545041). – SamR Aug 03 '22 at 08:20
  • Also this post [Dealing with TRUE, FALSE, NA and NaN](https://stackoverflow.com/questions/16822426/dealing-with-true-false-na-and-nan) – benson23 Aug 03 '22 at 08:22
  • ```NA``` has no value, if you compare it with a value as you did with ```v == 1```, you can't get a ```TRUE``` or ```FALSE``` back. A non-value can't be compared to a value so R will return a ```NA```. The close reason and other linked questions in the comments explain it more thorough. – Omniswitcher Aug 03 '22 at 08:30
  • Wrong duplicate, SamR's suggestion would be the right one. Additionally, if you use, `subset()`, `dplyr::filter()`, or data.table, the NAs will be excluded (and not everybody likes that). "it clearly doesn't meet the condition" is not true here, the opposite is true, we can't say if it meets the condition or not. – moodymudskipper Aug 03 '22 at 09:02

0 Answers0