R Subsetting Specific Value Also Returns NA?

这一生的挚爱 提交于 2019-12-11 17:18:59

问题


I am just starting out on learning R and came across a piece of code as follows

vec_1 <- c("a","b", NA, "c","d")

# create a subet of all elements which equal "a"
vec_1[vec_1 == "a"]

The result from this is

## [1] "a" NA

Im just curious, since I am subsetting vec_1 for the value "a", why does NA also show up in my results?


回答1:


This is because the result of anything == NA is NA. Even NA == NA is NA.

Here's the output of vec_1 == "a" -

[1] TRUE FALSE NA FALSE FALSE

and NA is not TRUE or FALSE so when you subset anything by NA you get NA. Check this out -

vec_1[NA]
[1] NA NA NA NA NA

When dealing with NA, R tries to provide the most informative answer i.e. T | NA returns TRUE because it doesn't matter what NA is. Here are some more examples -

T | NA
[1] TRUE

F | NA
[1] NA

T & NA
[1] NA

F & NA
[1] FALSE

R has no way to test equality with NA. In your case you can use %in% operator -

5 %in% NA
[1] FALSE

"a" %in% NA
[1] FALSE

vec_1[vec_1 %in% "a"]
[1] "a"


来源:https://stackoverflow.com/questions/52809897/r-subsetting-specific-value-also-returns-na

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!