[R] stopifnot with logical(0)

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Dec 11 16:23:09 CET 2015

The goal of the comparison operators is to obtain a logical value.  Why compare logical values... you clearly already have that?

stopifnot( logicalvariable ) and 
stopifnot( !logicalvariable )

are sensible,  but not

stopifnot( logicalvariable == TRUE ) or 
stopifnot( logicalvariable == FALSE )

That said, R lets you construct the department of redundancy department  anyway,  but you have to have a value to compare.  logical(0) is the absence of a logical value, so there is nothing to compare, so the result has to also be the absence of a logical value (logical(0)).

Somewhere, the test that lead to the existence of this empty logical vector had no data. That absence of data is what you need to test for. Or, at the very least, you need to verify that the length of your logical variable is greater than zero before checking its value. 
Sent from my phone. Please excuse my brevity.

On December 11, 2015 5:38:47 AM PST, Dario Beraldi <dario.beraldi at gmail.com> wrote:
>Hi All,
>I'd like to understand the reason why stopifnot(logical(0) == x)
>(never?) throw an exception, at least in these cases:
>stopifnot(logical(0) == 1)
>stopifnot(logical(0) == TRUE)
>stopifnot(logical(0) == FALSE)
>My understanding is that logical(0) is an empty set, so I would expect
>above tests to fail.
>(I got bitten by this in a piece of code where "x" happened to be
>logical(0) and stopifnot didn't catch it)
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list