# [R] value matching %in% for a number pair

Duncan Murdoch murdoch.duncan at gmail.com
Sun Nov 13 03:01:27 CET 2016

```On 12/11/2016 8:36 PM, John wrote:
> Hi,
>
>    We can match one numerical value as follows
>> 3 %in% c(4,5)
>  FALSE
>> 3 %in% c(4,5,3)
>  TRUE
>
>    To see whether value pairs are identical,
>> identical(c(3,4), c(3,5))
>  FALSE
>> identical(c(3,4), c(3,4))
>  TRUE
>
>    Is there any way to test whether “A value pair is in a set of value
> pairs”? For example, can we test whether the pair (2,3) is identical to one
> of the pairs in the set S={(1,2), (4,3), (3,3), (2,3), (4,5)}?
>    In this case, the answer is yes because the 4th element of S is (2,3).
> Is there any simple way to code it? Thanks!

You'll have to type a long expression or write your own function for it.
Here's one way, if you store your set as a list:

inlist <- function(x, thelist)
any(sapply(thelist, identical, x))

For example:

> S <- list(c(1, 2), c(4, 3))
> inlist(c(4, 3), S)
 TRUE
> inlist(c(3, 4), S)
 FALSE

Duncan Murdoch

```