[R] all(logical(0)) and any(logical(0))

Duncan Murdoch dmurdoch at pair.com
Thu Apr 15 17:17:21 CEST 2004


On Thu, 15 Apr 2004 10:40:41 -0400, "Liaw, Andy" <andy_liaw at merck.com>
wrote :

>Dear R-help,
>
>I was bitten by the behavior of all() when given logical(0):  It is TRUE!
>(And any(logical(0)) is FALSE.)  Wouldn't it be better to return logical(0)
>in both cases?

As Rolf said, this behaviour makes sense.

>The problem surfaced because some un-named individual called randomForest(x,
>y, xtest, ytest,...), and gave y as a two-level factor, but ytest as just
>numeric vector.  I thought I check for that in my code by testing for
>
>if (!all(levels(y) == levels(ytest))) stop(...)

You should fix this by having

y <- as.factor(y)

and 

ytest <- as.factor(ytest)

somewhere earlier in your code, since your code assumes that they are
both factors, but doesn't enforce that in its input checks.  

Duncan Murdoch




More information about the R-help mailing list