Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for equality between all members of list

Tags:

list

r

What is the right syntax to check equality between all members of a list? I found that

do.call(all.equal, list(1,2,3))

returns TRUE.

My hunch is that it is checking equality within elements of a list, not between.

Ideally the solution would ignore the name of the element, but not those of the element components. For example, it should return TRUE given the input:

some_list <- list(foo = data.frame(a=1),
                  bar = data.frame(a=1))

But FALSE given the input:

some_other_list <- list(foo = data.frame(a=1),
                        bar = data.frame(x=1))

But I could live with a solution that was sensitive to the name of the element.


Edit: Clearly I need to review ?funprog. As a follow-up to the accepted answer, it's worth noting the following behavior:

Reduce(all.equal, some_list)  # returns TRUE

Reduce(all.equal, some_other_list) #returns [1] "Names: 1 string mismatch"

And for:

yet_another_list <- list(foo = data.frame(a=1),
                        bar = data.frame(x=2))
Reduce(all.equal, yet_another_list)
# returns: [1] "Names: 1 string mismatch"     "Component 1: Mean relative difference: 1"

Edit 2: My example was inadequate, and Reduce doesn't work in the case of 3 or more elements:

some_fourth_list <- list(foo = data.frame(a=1),
                  bar = data.frame(a=1),
                  baz = data.frame(a=1))
Reduce(all.equal, some_fourth_list) # doesn't return TRUE
like image 742
C8H10N4O2 Avatar asked Dec 16 '15 13:12

C8H10N4O2


People also ask

How do you check if all the elements of a list are equal?

Check if all elements in a list are identical or not using count() By counting the number of times the first element occurs in the list, we can check if the count is equal to the size of the list or not.

How do you compare two lists to equal?

Using Counter() , we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not.

How do you check if all the elements in an Arraylist are same?

allMatch() method. The allMatch() method returns true if all elements of the stream matches with the given predicate. It can be used as follows to check if all elements in a list are the same.

How do you check if all items in a list are equal Python?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.


1 Answers

1) IF the list has only two components as in the question then try this:

identical(some_list[[1]], some_list[[2]])
## [1] TRUE

2) or for a general approach with any number of components try this:

all(sapply(some_list, identical, some_list[[1]]))
## [1] TRUE

L <- list(1, 2, 3)
all(sapply(L, identical, L[[1]]))
## [1] FALSE

3) Reduce This is a bit verbose compared to the prior solution but since there was a discussion of Reduce, this is how it could be jmplemented:

Reduce(function(x, y) x && identical(y, some_list[[1]]), init = TRUE, some_list)
## [1] TRUE

Reduce(function(x, y) x && identical(y, L[[1]]), init = TRUE, L)
## [1] FALSE
like image 148
G. Grothendieck Avatar answered Oct 18 '22 18:10

G. Grothendieck