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

Rolf Turner rolf at math.unb.ca
Thu Apr 15 17:01:34 CEST 2004


Andy Liaw wrote:

> 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?

It seems to me that what R does is strictly speaking correct.
Anything you say about the members of the empty set is true.
If a set of logical entities is empty, then it is correct to
say that all members of that set are TRUE.  (Because you cannot
find a counter-example --- you cannot find a member of that
set which isn't TRUE.)

Likewise you can't find a member of that set which ***is*** TRUE
so the answer to the question ``Are any of these TRUE?'' is ``No.'',
i.e. ``any(logical(0))'' is FALSE.

So returning logical(0) in these cases is not strictly correct;
whether it would do any harm is not clear to me --- i.e. I can't
think of an example where it would cause harm.  And of course it
would guard against the Trap For Young Players that Andy Liaw
described.  However since it is not strictly correct, great caution
ought to be used.

				cheers,

					Rolf Turner
					rolf at math.unb.ca




More information about the R-help mailing list