Finding the index of an NA value in a vector [duplicate]

旧时模样 提交于 2020-05-23 06:12:10

问题


I have the following vector:

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

And I just want to know the index of the NA in the vector. If, for instance, I wanted to know the index of 7, the following code would work:

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

I find it odd that running the same code when trying to find the index of the NA does 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 will be much appreciated.

Edit

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


回答1:


You may want to try using the which and is.na functions in the the following manner:

which(is.na(x))
[1] 3


来源:https://stackoverflow.com/questions/23840193/finding-the-index-of-an-na-value-in-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!