[R] all(logical(0)) and any(logical(0))
Liaw, Andy
andy_liaw at merck.com
Thu Apr 15 17:50:18 CEST 2004
Thanks to Rolf, Thomas, Duncan & Doug for the explanations! It's one of
those things that I should have remembered from high school but clearly
didn't...
I've changed my code to:
[If y is factor:]
if (!is.null(ytest)) {
if (!is.factor(ytest)) stop("ytest must be a factor")
if (!all(levels(y) == levels(ytest)))
stop("y and ytest must have the same levels")
}
For Duncan: `y' can be either factor or numeric. That's how randomForest
determines whether it's a classification or regression problem.
Thanks again to all!
Andy
> From: Liaw, Andy
>
> 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?
>
> 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(...)
>
> but levels() on a non-factor returns NULL, and the comparison
> ended up being
> logical(0). Since all(logical(0)) is TRUE, the error is not flagged.
>
> Best,
> Andy
>
> Andy Liaw, PhD
> Biometrics Research PO Box 2000, RY33-300
> Merck Research Labs Rahway, NJ 07065
> mailto:andy_liaw at merck.com 732-594-0820
>
>
>
> --------------------------------------------------------------
> ----------------
> Notice: This e-mail message, together with any
> attachments,...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
> --------------------------------------------------------------
> ----------------
> Notice: This e-mail message, together with any attachments,
> contains information of Merck & Co., Inc. (One Merck Drive,
> Whitehouse Station, New Jersey, USA 08889), and/or its
> affiliates (which may be known outside the United States as
> Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as
> Banyu) that may be confidential, proprietary copyrighted
> and/or legally privileged. It is intended solely for the use
> of the individual or entity named on this message. If you
> are not the intended recipient, and have received this
> message in error, please notify us immediately by reply
> e-mail and then delete it from your system.
> --------------------------------------------------------------
> ----------------
>
More information about the R-help
mailing list