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?
== 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With