Great job! Now, if you want to know how many values are missing from a vector, you can use the
sum() function, which takes a single vector argument and returns the sum of its elements. When the argument is a logical vector,
FALSE is treated as 0, and
TRUE is treated as 1.
Thus, by passing in the logical vector returned by
is.na() to the
sum() function, we can easily determine how many values are missing from the original vector. This corresponds to the number of
TRUE values in the logical vector.
Let's use the same vector as in our previous example:
a <- c(2, 3, 4, 5, NA, NA). When we invoke
R returns 2. This is indeed the number of values that are missing from the vector a!