[R] stopifnot with logical(0)

Henrik Bengtsson henrik.bengtsson at gmail.com
Fri Dec 11 17:20:55 CET 2015


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.



More information about the R-help mailing list