Search for NA value index in vector

I have the following vector:

x <- c(3, 7, NA, 4, 8)

And I just want to know the index NAin the vector. If, for example, I wanted to know the index 7, the following code would work:

> which(x == 7)
[1] 2

It seems strange to me that running the same code when trying to find the index NAdoes not give me the desired result.

> which(x == NA)
integer(0)

I also tried the following, but it does not work:

>  which(x == "NA")
integer(0)

Your help would be greatly appreciated.

Edit

The question was answered by @ccapizzano, but can anyone explain why the codes above do not work?

+4
source share
1 answer

You can try to use the functions whichand is.naas follows:

which(is.na(x))
[1] 3
+15
source

Source: https://habr.com/ru/post/1541839/


All Articles