'all' inconsistent?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

'all' inconsistent?

Elizabeth Purdom
Hello,
I came across the following behavior, which seems illogical to me. I don't
know if it is a bug or if I'm missing something:

 > all(logical(0))
[1] TRUE
 > any(logical(0))
[1] FALSE
 > isTRUE(logical(0))
[1] FALSE

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. I can clearly work around it, but it seemed
like strange behavior to me.
Thanks,
Elizabeth Purdom
R 2.2.1, Windows XP

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Gabor Grothendieck
I think the corresponding question was already discussed in
the context of sum and in terms of this question one wants:

all(x) && all(y) to equal all(c(x,y))

including the case where x or y has zero length.

On 1/30/06, Elizabeth Purdom <[hidden email]> wrote:

> Hello,
> I came across the following behavior, which seems illogical to me. I don't
> know if it is a bug or if I'm missing something:
>
>  > all(logical(0))
> [1] TRUE
>  > any(logical(0))
> [1] FALSE
>  > isTRUE(logical(0))
> [1] FALSE
>
> 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. I can clearly work around it, but it seemed
> like strange behavior to me.
> Thanks,
> Elizabeth Purdom
> R 2.2.1, Windows XP
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Brian Ripley
In reply to this post by Elizabeth Purdom
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
>
> ______________________________________________
> [hidden email] 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,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Seth Falcon-2
On 29 Jan 2006, [hidden email] wrote:

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

I thought all the values are false, all none of them, because there
aren't any that are true:

any(logical(0))
[1] FALSE

I can see how someone might expect an error, or NA, or FALSE in the
above two cases.  It is harder for me to see when all(logical(0))
being TRUE would be useful.

glass-is-half-emptily-yours,

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Giovanni Petris

> I thought all the values are false, all none of them, because there
> aren't any that are true:
>
> any(logical(0))
> [1] FALSE
>

This is for the same reason why a product over an empty set of
factors is 1, and a sum over an empty set of terms is 0.

GP

--

 __________________________________________________
[                                                  ]
[ Giovanni Petris                 [hidden email] ]
[ Department of Mathematical Sciences              ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)               ]
[ http://definetti.uark.edu/~gpetris/              ]
[__________________________________________________]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Uwe Ligges
In reply to this post by Seth Falcon-2
Seth Falcon wrote:

> On 29 Jan 2006, [hidden email] wrote:
>
>
>>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.
>
>
> I thought all the values are false, all none of them, because there
> aren't any that are true:
>
> any(logical(0))
> [1] FALSE
>
> I can see how someone might expect an error, or NA, or FALSE in the
> above two cases.  It is harder for me to see when all(logical(0))
> being TRUE would be useful.

Current behaviour is consistent in so far that identical(all(x),
!any(!x)) is TRUE and definition of any() is obvious.

Uwe


>
> glass-is-half-emptily-yours,
>
> + seth
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Seth Falcon-2
On 30 Jan 2006, [hidden email] wrote:
> Current behaviour is consistent in so far that identical(all(x),
> !any(!x)) is TRUE and definition of any() is obvious.

That helps, thanks.  I'm not sure I've had enough coffee to continue,
but, for the set analogy I think we are saying:

logical(0) is the empty set {}.
Complement of {} is the universal set U.

Then !logical(0)  == !{} == U.  any(U) is TRUE, isn't it?  

I guess the real message is that you need to protect yourself by
testing for positive length first.

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Uwe Ligges
Seth Falcon wrote:

> On 30 Jan 2006, [hidden email] wrote:
>
>>Current behaviour is consistent in so far that identical(all(x),
>>!any(!x)) is TRUE and definition of any() is obvious.
>
>
> That helps, thanks.  I'm not sure I've had enough coffee to continue,
> but, for the set analogy I think we are saying:
>
> logical(0) is the empty set {}.
> Complement of {} is the universal set U.
>
> Then !logical(0)  == !{} == U.  any(U) is TRUE, isn't it?  

Hmmm, "!" is for *logical* negation, and indeed identical(logical(0),
!(logical(0))) is TRUE, hence my first statement holds.


> I guess the real message is that you need to protect yourself by
> testing for positive length first.

Yes, indeed.

Uwe



> + seth
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Peter Dalgaard
In reply to this post by Seth Falcon-2
Seth Falcon <[hidden email]> writes:

> On 30 Jan 2006, [hidden email] wrote:
> > Current behaviour is consistent in so far that identical(all(x),
> > !any(!x)) is TRUE and definition of any() is obvious.
>
> That helps, thanks.  I'm not sure I've had enough coffee to continue,
> but, for the set analogy I think we are saying:
>
> logical(0) is the empty set {}.
> Complement of {} is the universal set U.
>
> Then !logical(0)  == !{} == U.  any(U) is TRUE, isn't it?  
>
> I guess the real message is that you need to protect yourself by
> testing for positive length first.

This comes up repeatedly. Probably the most useful way of viewing
these empty sum/prod/any/all issues is that you want

sum(c(x,y)) == sum(x) + sum(y)
prod(c(x,y)) == prod(x) * prod(y)
any(c(x,y)) == any(x) | any(y)
all(c(x,y)) == all(x) & all(y)

even in the cases where x or y is empty, and of course the neutral
operations are

adding 0
multiplying by 1
or'ing with FALSE
and'ing with TRUE

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: 'all' inconsistent?

Bjørn-Helge Mevik
In reply to this post by Seth Falcon-2
Seth Falcon wrote:

> On 29 Jan 2006, [hidden email] wrote:
>
>> 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.
>
> I thought all the values are false, all none of them, because there
> aren't any that are true:
>
> any(logical(0))
> [1] FALSE

But they are, all none of them:

> all(!logical(0))
[1] TRUE

:-)

And there aren't any FALSE values either:

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

so it is only logical that all none of them are TRUE.  I love the
empty set! :-)

--
Bjørn-Helge Mevik

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html