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

Craig Wright picture Craig Wright · Sep 6, 2012 · Viewed 81.1k times · Source

I received the error

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

or

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

What causes this error message, and what does it mean?

On further inspection it seems that the value is NULL.

condition
## NULL

In order to deal with this error, how do I test for NULL values?

I expected that this would return TRUE, but I got an empty logical value:

condition == NULL
## logical(0)

Answer

GSee picture GSee · Sep 6, 2012

See ?NULL

You have to use is.null

‘is.null’ returns ‘TRUE’ if its argument is ‘NULL’ and ‘FALSE’ otherwise.

Try this:

if ( is.null(hic.data[[z]]) ) { print("is null")}

From section 2.1.6 of the R Language Definition

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.