if/while(condition){:参数长度为零时出错 [英] Error in if/while (condition) { : argument is of length zero
问题描述
我收到了错误
Error in if (condition) { : argument is of length zero
或
Error in while (condition) { : argument is of length zero
导致此错误消息的原因是什么?
进一步检查似乎该值是NULL
.
On further inspection it seems that the value is NULL
.
condition
## NULL
为了处理此错误,如何测试NULL值?
我希望它会返回TRUE
,但是我得到了一个空的逻辑值:
I expected that this would return TRUE
, but I got an empty logical value:
condition == NULL
## logical(0)
推荐答案
请参见?NULL
您必须使用is.null
如果"is.null"的参数为"NULL"和"FALSE",则返回"TRUE" 否则.
‘is.null’ returns ‘TRUE’ if its argument is ‘NULL’ and ‘FALSE’ otherwise.
尝试一下:
if ( is.null(hic.data[[z]]) ) { print("is null")}
来自 R语言定义的第2.1.6节
From section 2.1.6 of the R Language Definition
有一个特殊的对象,称为NULL.在需要指示或需要时使用 指定不存在对象.请勿将其与向量或零列表混淆 长度. NULL对象没有类型,也没有可修改的属性.只有一个NULL对象 在R中,所有实例都参考.要测试NULL,请使用is.null.您不能设置属性 NULL.
There is a special object called NULL. It is used whenever there is a need to indicate or specify that an object is absent. It should not be confused with a vector or list of zero length. The NULL object has no type and no modifiable properties. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL.
这篇关于if/while(condition){:参数长度为零时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!