First off, logical(0)
indicates that you have a vector that's supposed to contain boolean values, but the vector has zero length.
In your first approach, you do
!is.na(c(NA, NA, NA))
# FALSE, FALSE, FALSE
Using the which()
on this vector, will produce an empty integer vector (integer(0)
). Testing whether an empty set is equal to zero, will thus lead to an empty boolean vector.
In your second approach, you try to see whether the vector which(!is.na(c(NA,NA,NA))) == 0
is TRUE
or FALSE
. However, it is neither, because it is empty. The if
-statement needs either a TRUE
or a FALSE
. That's why it gives you an error argument is of length zero
if
clause fails. – Confessedlyif(any(!is.na(c(NA,NA,NA))) == 0){print('TRUE')}
– Excavatewhich
withsum
you will get the expected behavior, i.e.if(sum(!is.na(c(NA,NA,NA))) == 0){print('TRUE')}
– Kessler