我收到此错误消息:

Error in if (condition) { : missing value where TRUE/FALSE needed

要么
Error in while (condition) { : missing value where TRUE/FALSE needed

这是什么意思,我该如何预防?

最佳答案

condition的求值得到了NA。条件if必须具有TRUEFALSE结果。

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

另请参阅相关错误:

Error in if/while (condition) { : argument is of length zero

Error in if/while (condition) : argument is not interpretable as logical
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

关于r - if/while(condition){: missing Value where TRUE/FALSE needed,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22888438/

10-12 20:39