我收到此错误消息:
Error in if (condition) { : missing value where TRUE/FALSE needed
或者
Error in while (condition) { : missing value where TRUE/FALSE needed
这是什么意思,我该如何预防?
评估condition结果为NA. if条件必须有 aTRUE或结果FALSE。
condition
NA
if
TRUE
FALSE
if (NA) {} ## Error in if (NA) { : missing value where TRUE/FALSE needed
作为计算结果,这可能会意外发生:
if(TRUE && sqrt(-1)) {} ## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
要测试一个对象是否丢失,请使用is.na(x)而不是x == NA.
is.na(x)
x == NA
if (NULL) {} ## Error in if (NULL) { : argument is of length zero if ("not logical") {} ## Error: argument is not interpretable as logical if (c(TRUE, FALSE)) {} ## Warning message: ## the condition has length > 1 and only the first element will be used