Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tests for the equality of empty Boolean sets

Tags:

r

logic

In R there is one comparison which seems odd:

> logical(0) == logical(0)
logical(0)


> logical(0) != logical(0)
logical(0)

From the mathematical equality of empty sets, it appears these two statements should evaluate to TRUE and FALSE. Why instead do these two statements evaluate to an empty set?

like image 803
Agriculturist Avatar asked Dec 06 '25 05:12

Agriculturist


1 Answers

== and != perform element wise comparisons. The result should have the same length as the inputs (after recycling if necessary) so the result should be of length 0 since they're both of length 0.

It isn't asking if the two sets/vectors as a whole are equal. If you want that then you should use identical or all.equal depending on your particular use case.

> identical(logical(0), logical(0))
[1] TRUE

Edit: It occured to me that instead of identical or all.equal you might be interested in set equality. If you want to test if two vectors contain the same values you can use setequal.

> setequal(1:3, 1:2)
[1] FALSE
> # order doesn't matter for sets
> setequal(c(1,2), c(2,1))
[1] TRUE
> setequal(logical(0), logical(0))
[1] TRUE
like image 190
Dason Avatar answered Dec 08 '25 20:12

Dason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!