Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Comparing two lists for exact equality

Tags:

list

r

I have two lists:

list_1 = list(2, 3, 5)
list_2 = list(2, 3, 5)

How can I find out if the 2 lists are exactly equal. It can be done in hard way too, but is there a simple way to do it.

like image 897
Rohit Singh Avatar asked Apr 16 '16 13:04

Rohit Singh


People also ask

How do I check if two lists are equal in R?

Compare equal elements in two R lists Third, you can also compare the equal items of two lists with %in% operator or with the compare. list function of the useful library. The result will be a logical vector.

How do you find the equality of two lists?

Using list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.

How do you check if two lists are equal without orders?

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. But this method also ignores the ordering of the elements in the list and only takes into account the frequency of elements.

Which function is used to compare two lists?

The cmp() function is used to compare two elements or lists and return a value based on the arguments passed.


2 Answers

Just for this task is the function identical()

list_1 = list(2, 3, 5)
list_2 = list(2, 3, 5)
identical(list_1, list_2)
# [1] TRUE

but

list_1 = list(2, 3, 5)
list_2 = list(2, 5, 3)
identical(list_1, list_2)
# [1] FALSE

and

list_1 = list(2, 3, 5)
list_2 = list(2, 3L, 5)
identical(list_1, list_2)
# [1] FALSE
like image 182
jogo Avatar answered Sep 26 '22 03:09

jogo


identical(list_1,list_2)

should work.

like image 23
Ankur Avatar answered Sep 22 '22 03:09

Ankur