Question Details

No question body available.

Tags

r

Answers (3)

January 17, 2026 Score: 5 Rep: 230,572 Quality: Medium Completeness: 50%

It's very tricky to figure out all the possible interactions of NULL and NA. Since NULL technically has length zero/zero elements, it makes sense for it to return a length-zero vector.

You haven't given us a reproducible example, so it's a little hard to be sure, but it sounds like what you need is the || operator, which "short-circuits" (returns early) if its first argument is TRUE:

x
January 17, 2026 Score: 2 Rep: 33,921 Quality: Medium Completeness: 80%

Internally in doisna (see coerce.c~L2260), the output is allocated with allocVector(LGLSXP, n) where n = xlength(x). Since NULL (RNilValue) has length 0, you get logical(0) back: zero elements in, zero elements out.

Your is.null(x) | is.na(x) fails because | is vectorized with recycling: length-1 (TRUE) combined with length-0 (logical(0)) yields length-0.


I'd suggest using anyNA() and ||:

if (is.null(x) || anyNA(x)) {
  ...
  ...
  ...
}

anyNA() always returns a scalar. In case of NULL where input length is 0, it returns FALSE (see coerce.c~L2370). The || avoids evaluating anyNA(x) if is.null(x) is already TRUE.

January 17, 2026 Score: 0 Rep: 78,049 Quality: Low Completeness: 70%

If you want is.na(NULL) to return FALSE try testing its return value with isTRUE.

isTRUE(is.na(NULL))
#> [1] FALSE

Created on 2026-01-17 with reprex v2.1.1

From the documentation help("isTRUE"):

isTRUE(x) is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }; isFALSE() is defined analogously. Consequently, if(isTRUE(cond)) may be preferable to if(cond) because of NAs.
In earlier R versions, isTRUE