There are two ways to calculate the average without the missing values.
The first option is to use the option na.rm = TRUE
in the mean function.
mean(na_example, na.rm = T)
#> [1] 2.301754
However, this option is specific to the mean
function and is less general than the solution you will be coming up in this exercise.
Store your result (the average without missing values) in vector_mean
.
We give you two hints how you can approach this.
Hint: you can subset a vector using a logical vector as index vector[logical]
.
Hint: Use the !
operator, which inverts your result of your logical vector.
Have a look at the following example.
original <- c(TRUE, TRUE, FALSE)
inverted <- !original
inverted
#> FALSE FALSE TRUE