Hi All, I'd like to understand the reason why stopifnot(logical(0) == x) doesn't (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 the 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) Thanks! Dario [[alternative HTML version deleted]]
The reason is probably that any(logical()) and any(!logical()) return FALSE (there are no TRUEs in logical(0)). Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Dec 11, 2015 at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote:> Hi All, > > I'd like to understand the reason why stopifnot(logical(0) == x) doesn't > (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 the > 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) > > Thanks! > Dario > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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) >doesn't >(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 >the >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) > >Thanks! >Dario > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: > > Hi All, > > I'd like to understand the reason why stopifnot(logical(0) == x) doesn't > (never?) throw an exception, at least in these cases:The usual way to test for a length-0 logical object is to use length(): x <- logical(0) stopifnot( !length(x) & mode(x)=="logical" )> > 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 the > 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) > > Thanks! > Dario-- David Winsemius Alameda, CA, USA
On Fri, Dec 11, 2015 at 8:10 AM, David Winsemius <dwinsemius at comcast.net> wrote:> >> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: >> >> Hi All, >> >> I'd like to understand the reason why stopifnot(logical(0) == x) doesn't >> (never?) throw an exception, at least in these cases: > > The usual way to test for a length-0 logical object is to use length(): > > x <- logical(0) > > stopifnot( !length(x) & mode(x)=="logical" )I found stopifnot(!length(x), mode(x) == "logical") more helpful when troubleshooting, because it will tell you whether it's !length(x) or mode(x) == "logical" that is FALSE. It's as if you wrote: stopifnot(!length(x)) stopifnot(mode(x) == "logical") /Henrik> > >> >> 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 the >> 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) >> >> Thanks! >> Dario > -- > > David Winsemius > Alameda, CA, USA > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.