What is the difference between NULL and character(0) | integer(0) etc?
> identical(NULL, character(0))
[1] FALSE
> is.null(integer(0))
[1] FALSE
> str(character(0))
chr(0)
> str(NULL)
NULL
In general it seems you can pass NULL as parameters into functions, and that an empty vector is generally returned as character(0), integer(0), etc.
Why is this the case? Come to think of it, is there a test for zero-ness, a la is.integer0?
The R Language Definition has this on
NULL:So by definition
NULLis very different to zero length vectors. A zero length vector very much isn’t absent.NULLis really a catch-all for something that is absent or not set, but not missing-ness, which is the job ofNA. There is an exception, the zero-length pairlist, as mentioned by @Owen. The Language Definition states:which highlights the exception in this case.
To test for a zero-length vector use something like
if(length(foo) == 0L)for example. And combine that with a class check (is.character(foo)) if you want a specific type of zero length vector.