Something weird about returning NAs

浪子不回头ぞ 提交于 2019-12-11 09:45:15

问题


this is a lame question I guess, but i don't understand what's is happining. If I go:

sum(is.na(census$wd))

It returns 4205

But if I go with:

sum(census$wd == NA)

It returns "NA"

I just would like to understand whats is happening. If I do str(census), wd shows up as:

$ wd         : num  NA 0.65 0.65 0.65 0.78 0.78 0.78 0.78 0.78 0.78 ...

Can anyone explains why the codes return different outputs? Thank you!


回答1:


== in R is a comparison. But you can not compare something to NA in Ras the following quote from ?Comparison states:

Missing values (NA) and NaN values are regarded as non-comparable even to themselves, so comparisons involving them will always result in NA.

In contrast is.na indicates which elements are missing regardless of their type. So it returns a vector of TRUE and FALSE entries.

> a <- c(NA,1,2,3)
> a == NA
[1] NA NA NA NA

> is.na(a)
[1]  TRUE FALSE FALSE FALSE

this is why sum is working with is.na (interpreting TRUE=1 and FALSE=0 but it cannot sum up a vector of NA's (generated by ==NA)



来源:https://stackoverflow.com/questions/25274518/something-weird-about-returning-nas

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