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.
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.
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.
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.
The cmp() function is used to compare two elements or lists and return a value based on the arguments passed.
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
identical(list_1,list_2)
should work.
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