'Mean removing NAs inside vectors in R with ! operator

I am stuck with a problem in R. It is about removing NAs within vectors and dataframes.

I am given the library, data frame and the vector as follows:

library(dslabs)
data(na_example)
ind <- is.na(na_example)

So, I need to compute the mean, but with the entries that are not NA inside the vector "ind".

I have tried everything, including the answer (I think) that is: mean(!ind), because I HAVE to use the ! operator.

The result is 0.855. However, the evaluating system does not give me a positive score.

Please, could you give me a hand?

rna


Solution 1:[1]

You're looking for na.omit, not is.na:

library(dslabs)
data(na_example)
ind <- na.omit(na_example)

mean(ind)

Which gives you: 2.301754

Solution 2:[2]

So, I finally got after many hours of struggle.

I was putting the ! in the wrong place

ind <- is.na(na_example)
mean(!ind)
[1] 0.855

It should be:

ind <- !is.na(na_example)
mean(ind)
[1] 0.855

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt
Solution 2 benson23