== 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 )
source share