if/while(条件){:缺少TRUE/FALSE所需的值。

时间:2022-09-10 23:27:11

I received this error message:

我收到了这个错误信息:

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

or

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

What does it mean, and how do I prevent it?

这意味着什么,我该如何预防呢?

2 个解决方案

#1


137  

The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.

对条件的评价导致了一个NA。如果条件必须有一个真或假的结果。

if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed

This can happen accidentally as the results of calculations:

这可能是偶然的,因为计算结果:

if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed

To test whether an object is missing use is.na(x) rather than x == NA.

为了测试一个对象是否丢失了使用is.na(x)而不是x == NA。


See also the related errors:

参见相关错误:

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

if/while(条件){:参数为长度为零的错误。

Error in if/while (condition) : argument is not interpretable as logical

if/while(条件):参数不能作为逻辑解释。

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

#2


3  

I ran into this when checking on a null or empty string

我在检查null或空字符串时遇到了这个问题。

if (x == NULL || x == '') {

changed it to

把它改为

if (is.null(x) || x == '') {

#1


137  

The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.

对条件的评价导致了一个NA。如果条件必须有一个真或假的结果。

if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed

This can happen accidentally as the results of calculations:

这可能是偶然的,因为计算结果:

if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed

To test whether an object is missing use is.na(x) rather than x == NA.

为了测试一个对象是否丢失了使用is.na(x)而不是x == NA。


See also the related errors:

参见相关错误:

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

if/while(条件){:参数为长度为零的错误。

Error in if/while (condition) : argument is not interpretable as logical

if/while(条件):参数不能作为逻辑解释。

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

#2


3  

I ran into this when checking on a null or empty string

我在检查null或空字符串时遇到了这个问题。

if (x == NULL || x == '') {

changed it to

把它改为

if (is.null(x) || x == '') {