Something strange about the return of the National Assembly

This is an uncomfortable question, but I do not understand what is happening. If I go:

sum(is.na(census$wd)) 

He returns 4205

But if I go:

 sum(census$wd == NA) 

He returns "NA"

I just wanted to understand what was going on. If I do str (census), wd is displayed as:

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

Can someone explain why the codes return different outputs? Thanks!

+1
source share
1 answer

== in R is a comparison. But you cannot compare something with NA in R since the following quote from ?Comparison reads:

Missing values ​​(NA) and NaN are considered incompatible even for themselves, so comparisons with them will always lead to NA.

Unlike is.na , which elements are missing regardless of their type. Thus, it returns the vector of records TRUE and FALSE .

 > 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 works with is.na (interpreting TRUE=1 and FALSE=0 , but cannot sum the vector NA (generated ==NA )

+3
source

Source: https://habr.com/ru/post/1200293/


All Articles