This line:
which(!is.na(c(NA,NA,NA))) == 0
produces logical(0)
While this line
if(which(!is.na(c(NA,NA,NA))) == 0){print('TRUE')}
generates:
Error in if (which(!is.na(c(NA, NA, NA))) == 0) { :
argument is of length zero
Why there is an error? What is logical(0)
"Logic 0" and "logic 1" represent binary digits (0 and 1) or Boolean logic conditions (true and false). Thinking in terms of logic 0 and logic 1 allow engineers to design circuits and logic gates at a high level of abstraction that is removed from implementation considerations.
In Map Algebra, any non-zero input value is considered to be a logical true, and zero is considered a logical false. Some Map Algebra operators and functions evaluate input cell values and return logical 1 values (true) and logical 0 values (false). The relational and Boolean operators all return logical values.
First off, logical(0) indicates that you have a vector that's supposed to contain boolean values, but the vector has zero length.
NULL is a "zero-length" object, so any elementwise comparison or operation with NULL will have length zero: logical(0) represents a logical vector of length zero. You might find identical() useful: identical(NULL,NULL) is TRUE, identical(NULL,NA) is FALSE.
logical(0)
is a vector of base type logical with 0 length. You're getting this because your asking which elements of this vector equal 0:
> !is.na(c(NA, NA, NA))
[1] FALSE FALSE FALSE
> which(!is.na(c(NA, NA, NA))) == 0
logical(0)
In the next line, you're asking if that zero length vector logical(0)
is equal to 0, which it isn't. You're getting an error because you can't compare a vector of 0 length with a scalar.
Instead you could check whether the length of that first vector is 0:
if(length(which(!is.na(c(NA,NA,NA)))) == 0){print('TRUE')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With