Skip to content

all.equal and use.names

6 messages · Bert Gunter, John Harrold, Eric Berger +1 more

#
Howdy Folks,

I believe I'm having trouble understanding the documentation for all.equal.
If I have two lists like this:

t1 = list(a = c(1,2,3),
          b = c("1", "2", "3"))
t2 = list( b = c("1", "2", "3"),
           a = c(1,2,3))

If I read the documentation correctly, by setting use.names equal to TRUE I
believe this comparison should evaluate as true:

all.equal(t1,t2, use.names=TRUE)

However, I get the following output:

which appears as though it is performing the comparison based on walking
through indices and comparing that way.

[1] "Names: 2 string mismatches"
[2] "Component 1: Modes: numeric, character"
[3] "Component 1: target is numeric, current is character"
[4] "Component 2: Modes: character, numeric"
[5] "Component 2: target is character, current is numeric"

Can someone tell me what I'm doing wrong here?
#
Nope. You misread I think. It says that use.names = TRUE causes mismatches
to be **reported** by name rather than index, not that it is recursing by
name. It still recurses by component indices.

However, I still think that is wrong. It is not reporting mismatches **by**
name -- it is reporting mismatches **in** names as well as in value.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 8:23 PM John Harrold <john.m.harrold at gmail.com>
wrote:

  
  
#
Is there a way to compare t1 and t2 above such that the name is used
instead of the index?
On Wed, May 27, 2020 at 9:14 PM Bert Gunter <bgunter.4567 at gmail.com> wrote:

            

  
    
#
Why not just reorder the elements of the list so they should match?

t1 <- t1[ names(t1)[order(names(t1))]]
t2 <- t2[ names(t2)[order(names(t2))]]

identical(t1,t2)




On Thu, May 28, 2020 at 7:52 AM John Harrold <john.m.harrold at gmail.com>
wrote:

  
  
#
Note:  all.equal() with all its S3 methods is implemented entirely in R
       code, so it should not be hard to find out where things happen
       and how.
> Is there a way to compare t1 and t2 above such that the
    > name is used instead of the index?

I think that may be a reasonable feature request.
If you sit down look at the R codes and muse a bit, you may even get to propose
a new optional argument to the all.equal.list() method.
Note the relevant R code is all in <R>/src/library/base/R/all.equal.R
development version (true source!) at
   https://svn.r-project.org/R/trunk/src/library/base/R/all.equal.R

--> would be a topic for R-devel (rather than R-help) though.

Best,
Martin

    > On Wed, May 27, 2020 at 9:14 PM Bert Gunter
> <bgunter.4567 at gmail.com> wrote:
>> Nope. You misread I think. It says that use.names = TRUE
    >> causes mismatches to be **reported** by name rather than
    >> index, not that it is recursing by name. It still
    >> recurses by component indices.
    >> 
    >> However, I still think that is wrong. It is not reporting
    >> mismatches **by** name -- it is reporting mismatches
    >> **in** names as well as in value.
    >> 
    >> 
    >> Bert Gunter
    >> 
    >> "The trouble with having an open mind is that people keep
    >> coming along and sticking things into it."  -- Opus (aka
    >> Berkeley Breathed in his "Bloom County" comic strip )
    >> 
    >> 
    >> On Wed, May 27, 2020 at 8:23 PM John Harrold
>> <john.m.harrold at gmail.com> wrote:
>> 
    >>> Howdy Folks,
    >>> 
    >>> I believe I'm having trouble understanding the
    >>> documentation for all.equal.  If I have two lists like
    >>> this:
    >>> 
    >>> t1 = list(a = c(1,2,3), b = c("1", "2", "3")) t2 = list(
    >>> b = c("1", "2", "3"), a = c(1,2,3))
    >>> 
    >>> If I read the documentation correctly, by setting
    >>> use.names equal to TRUE I believe this comparison should
    >>> evaluate as true:
    >>> 
    >>> all.equal(t1,t2, use.names=TRUE)
    >>> 
    >>> However, I get the following output:
    >>> 
    >>> which appears as though it is performing the comparison
    >>> based on walking through indices and comparing that way.
    >>> 
    >>> [1] "Names: 2 string mismatches" [2] "Component 1:
    >>> Modes: numeric, character" [3] "Component 1: target is
    >>> numeric, current is character" [4] "Component 2: Modes:
    >>> character, numeric" [5] "Component 2: target is
    >>> character, current is numeric"
    >>> 
    >>> Can someone tell me what I'm doing wrong here?
    >>> --
    >>> John :wq
    >>> 
    >>> [[alternative HTML version deleted]]
    >>> 
    >>> ______________________________________________
    >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and
    >>> more, see https://stat.ethz.ch/mailman/listinfo/r-help
    >>> PLEASE do read the posting guide
    >>> http://www.R-project.org/posting-guide.html and provide
    >>> commented, minimal, self-contained, reproducible code.
    >>> 
    >> 

    > -- 
    > John :wq

    > 	[[alternative HTML version deleted]]

    > ______________________________________________
    > R-help at r-project.org mailing list -- To UNSUBSCRIBE and
    > more, see https://stat.ethz.ch/mailman/listinfo/r-help
    > PLEASE do read the posting guide
    > http://www.R-project.org/posting-guide.html and provide
    > commented, minimal, self-contained, reproducible code.
#
Howdy Folks,

Eric - I'm just using this as a toy example to illustrate what I'm trying
to do. The actual lists I'm trying to compare are much larger and more
complex. So sorting them like that is something I'd have to do recursively.
I wanted to take advantage of all.equal because I want to be able to
identify the elements that are missing in one vs the other. And my
understanding is that identical() won't give me that kind of out put.

I think what I'm going to do is flatten both lists using unlist, and then
compare them using the names of the two flattened lists.

Thanks
John

On Thu, May 28, 2020 at 1:54 AM Martin Maechler <maechler at stat.math.ethz.ch>
wrote: