R check if list of lists contains specific list

Viewed 225

There are three lists:

a = list(1,2)
b = list(2,3)
c = list(a,b)

The command a %in% c yields FALSE FALSE. The result I would like to see is TRUE since a is an element of list c. How do I achieve this?

2 Answers

Check whether each component is identical to a and return TRUE if any of those comparisons are TRUE.

any(sapply(c, identical, a))
## [1] TRUE

This should also help:

list(a) %in% c

Examples:

a = list(1,2)
b = list(2,3)
c = list(a,b)
y = list(3,4)
z = list(1)


list(a) %in% c  # True
list(b) %in% c  # True
list(y) %in% c  # False
list(z) %in% c  # False
Related