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

Douglas Bates bates at stat.wisc.edu
Thu Apr 15 17:34:35 CEST 2004


"Liaw, Andy" <andy_liaw at merck.com> writes:

> I 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?
> 
> I guess the behavior is consistent with:
> 
> > prod(numeric(0))
> [1] 1
> > sum(numeric(0))
> [1] 0
> 
> but why?

The operation applied to a zero-length vector returns the identity
element of the operator.  The identity element of * is 1, of + is 0,
of & is TRUE and of | is FALSE.




More information about the R-help mailing list