The na_example
vector represents a series of counts. You can quickly examine the object using:
library(dslabs)
data("na_example")
str(na_example)
#> int [1:1000] 2 1 3 2 1 3 1 4 3 2 ...
Or have a look at the first 50 values with:
head(na_example, n = 50)
#> [1] 2 1 3 2 1 3 1 4 3 2 2 NA 2 2 1 4 NA 1 1 2 1 2 2 1 2 5 NA 2 2 3 1 2 4 1 1 1 4 5 2 3 4 1 2 4 1 1 2 1 5 NA
We observe that not all the values in the vector contain an integer number, some of them are missing values, denoted by NA
in R.
When we compute the average with the function mean
, we obtain an NA
result as well:
mean(na_example)
#> [1] NA
The mean
function requires input without missing values.
We will tackle this in the next exercise, first we want to find out how many missing values we have.
The is.na
function returns a logical vector that tells us which entries are NA
.
Assign this logical vector to an object called ind
and determine how many NA
s does na_example
have.
Store your anwser in na_count
.
Hint: you can use the sum
function to count the number of occurences of TRUE
in a logical vector.
The TRUE
values will be counted as 1 and the FALSE
values as 0.