Interesting, right? The resulting data frame displays NA
s for missing records! This is because R does not know how to compare NA
with 500.
Obviously, missing values are problematic in analysis, so it's usually best to somehow remove them. Only then can we work on further analyzing the data. But if we can't use operators when one of the operands is NA
, how are we going to detect or display missing values? Well, as usual, there's a function for that!
The is.na()
function takes a single vector argument and returns a logical vector populated with TRUE
and FALSE
values. An element in the logical vector returned by is.na()
will only be TRUE
if the corresponding element in the original vector is NA
. It will be FALSE
otherwise. Here's an example of a vector with some missing values:
a <- c(2, 3, 4, 5, NA, NA)
The expression is.na(a)
will return the following:
[1] FALSE FALSE FALSE FALSE TRUE TRUE