R-<需要TRUE/FALSE的缺失值“ [英] R - " missing value where TRUE/FALSE needed "

查看:111
本文介绍了R-<需要TRUE/FALSE的缺失值“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中执行以下代码

I'm trying to execute the following code in R

comments = c("no","yes",NA)
for (l in 1:length(comments)) {
    if (comments[l] != NA) print(comments[l]);
}

但是我遇到了错误

Error in if (comments[l] != NA) print(comments[l]) : missing value where TRUE/FALSE needed

这是怎么回事?

推荐答案

检查命令:NA!=NA:您将得到结果NA,因此出现错误消息.

check the command : NA!=NA : you'll get the result NA, hence the error message.

您必须使用函数is.na来使if语句正常工作(通常,最好使用此函数来检查NA值):

You have to use the function is.na for your ifstatement to work (in general, it is always better to use this function to check for NA values) :

comments = c("no","yes",NA)
for (l in 1:length(comments)) {
    if (!is.na(comments[l])) print(comments[l])
}
[1] "no"
[1] "yes"

这篇关于R-<需要TRUE/FALSE的缺失值“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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