R-“;缺少需要TRUE/FALSE的值“;
我试图在R中执行以下代码R-“;缺少需要TRUE/FALSE的值“;,r,if-statement,syntax,R,If Statement,Syntax,我试图在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 这是怎么回事 能否将if条件更改为: if (!is.n
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
这是怎么回事 能否将if条件更改为:
if (!is.na(comments[l])) print(comments[l]);
只能使用is.NA()检查NA值。检查命令:
NA=NA
:您将得到结果NA,因此会显示错误消息
必须使用函数is.na
才能使if
语句正常工作(通常,最好使用此函数检查na
值):
谢谢你,麻烦了solved@user3582590请考虑接受实际回答你的问题的答案,点击在答案的投票计数下的小复选标记。这为未来的读者确定了最有用的答案……请用is.NA()检查NA,在我的例子中,接受残酷的现实,即它不是NULL,并且NA和NULL都存在。@Chris有一个
is.NULL
函数;-)
comments = c("no","yes",NA)
for (l in 1:length(comments)) {
if (!is.na(comments[l])) print(comments[l])
}
[1] "no"
[1] "yes"