[R] Compare objects

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Nov 10 01:04:24 CET 2008


Peter Dalgaard wrote:
> Heinz Tuechler wrote:
>> At 13:26 09.11.2008, Leon Yee wrote:
>>> Hi, friends
>>>
>>>    Is there any functions for object comparing? For example, I have two
>>> list objects, and I want to know whether they are the same. Since the
>>> the components of list are not necessary atomic, this kind of
>>> comparison
>>> should be recursive. Does this kind of function exist?
>>>    Thank you for your help!
>>>
>>> Leon
>>
>> see maybe:
>> all.equal()
>> identical()
>
> ...and don't miss the compare package described in issue 8/2 of R News.
>

... and don't miss the fact that none of these tests sameness, in the
sense of two identifiers referring to the same object, as opposed to two
indistinguishable (by some criteria) but distinct objects.  since you
mentioned recursive testing, you probably wanted structural equality,
and that's what identical and compare (with equal=FALSE) do.

sameness (or identity, as in logic) is a relation that holds exclusively
between an object and itself.  sameness holds for no *two* objects. 
identical does not test sameness (and neither does it test identity in
the logical sense):

x = 1:2
y = 1:2
identical(x,y)
# true, but not because x and y are the same

(x[1] = 0)
# has changed
y
# has not changed

x and y are *two* distinct objects, but identical does *not* handle
this.  neither does compare. 


interesting to note that the man page for identical does not use the
terms 'identical' and 'identity', but rather 'equal' and 'equality',
thus 'identical' is a clear misnomer in this context.  also, the option
equal in compare is a misnomer, and its explanation is confused:

"test for equality if test for identity fails" -- 'identity' here seems
to mean what 'equality' means for identical, as explained on that man page:

identical(1, 1:1)
# false, they are not equal (as identical's man page explains)

compare(1, 1:1, equal=FALSE)
# false, identity test fails (as compare's man page explains)

compare(1, 1:1, equal=TRUE)
# true, they are equal (as compare's man page explains)


oops, clearly a terminological mess.

vQ



More information about the R-help mailing list