Error in if (x[i] == NA) { : missing value where TRUE/FALSE needed
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Error in if (x[i] == NA) { : missing value where TRUE/FALSE needed相关的知识,希望对你有一定的参考价值。
Error in if (x[i] == NA) : missing value where TRUE/FALSE needed
目录
Error in if (x[i] == NA) : missing value where TRUE/FALSE needed
问题:
x[i] == NA这个语法是有问题的;
正确的方式是:is.na(x[i])
#define vector with some missing values
x <- c(2, NA, 5, 6, NA, 15, 19)
#loop through vector and print "missing" each time an NA value is encountered
for(i in 1:length(x))
if (x[i] == NA)
print('Missing')
解决:
is.na(x[i])
#define vector with some missing values
x <- c(2, NA, 5, 6, NA, 15, 19)
#loop through vector and print "missing" each time an NA value is encountered
for(i in 1:length(x))
if (is.na(x[i]))
print('Missing')
完整错误:
> #define vector with some missing values
> x <- c(2, NA, 5, 6, NA, 15, 19)
>
> #loop through vector and print "missing" each time an NA value is encountered
> for(i in 1:length(x))
+
+ if (x[i] == NA)
+ print('Missing')
+
+
Error in if (x[i] == NA) : missing value where TRUE/FALSE needed
>
以上是关于Error in if (x[i] == NA) { : missing value where TRUE/FALSE needed的主要内容,如果未能解决你的问题,请参考以下文章
SThw2——find the error in the follow case
R Error in :missing values are not allowed in subscripted assignments of data frames
R Error in :missing values are not allowed in subscripted assignments of data frames
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
Error in unoecdf(cc, pdiff[case], Wi[case] * PTB.Vi[case]) : NA/NaN/Inf in foreign function call