查找向量中NA值的索引 [英] Finding the index of an NA value in a vector

查看:85
本文介绍了查找向量中NA值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下向量:

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?

推荐答案

您可能希望以以下方式尝试使用whichis.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆