[R] 'all' inconsistent?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 30 08:52:59 CET 2006


On Sun, 29 Jan 2006, Elizabeth Purdom wrote:

> I came across the following behavior, which seems illogical to me.

What did you expect and why?

> I don't know if it is a bug or if I'm missing something:
>
> > all(logical(0))
> [1] TRUE

All the values are true, all none of them.

> > any(logical(0))
> [1] FALSE

There are no true values here.

> > isTRUE(logical(0))
> [1] FALSE

This one I had to look up.  The help page says

    'isTRUE(x)' is an abbreviation of 'identical(TRUE,x)'.

so it means isTRUE() is true if and only if the result is a logical vector 
of length one and value TRUE (and with no attributes)
E.g.

> xx <- TRUE
> isTRUE(xx)
[1] TRUE
> names(xx) <- "a"
> isTRUE(xx)
[1] FALSE

That could use a little more explanation on the help page.

> This actually came up in practice when I did something like
> > all( names(x) %in% vec )
> as an error-handling, and I was hoping that it would work regardless of
> whether x had names or not.

Depends what `work' means here.  It is true if and only if all the names 
of 'x' are in 'vec', which is presumably not what you wanted.

> I can clearly work around it, but it seemed
> like strange behavior to me.
> Thanks,
> Elizabeth Purdom
> R 2.2.1, Windows XP
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list