查找向量中NA值的索引 [英] Finding the index of an NA value in a vector
问题描述
我有以下向量:
x <- c(3, 7, NA, 4, 8)
我只想知道向量中NA
的索引.例如,如果我想知道7
的索引,则下面的代码将起作用:
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
我感到奇怪的是,尝试查找NA
的索引时运行相同的代码并没有给我想要的结果.
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.
编辑
下面的@ccapizzano回答了这个问题,但是谁能解释为什么上面的代码不起作用?
The question has been answered below by @ccapizzano, but can anyone explain why the codes above do not work?
推荐答案
您可能希望以以下方式尝试使用which
和is.na
函数:
You may want to try using the which
and is.na
functions in the the following manner:
which(is.na(x))
[1] 3
这篇关于查找向量中NA值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!