Match list elements to another list

match_list(x, y, ...)

Arguments

x, y

list objects to be compared

...

additional arguments are ignored.

Value

integer

vector with the same length as x. The integer values give the position in y of the first match.

Details

This function takes two list objects, and matches the first list elements to the second list.

Each list contains list elements, for example if x is a list, then the element in position i is accessed using x[[i]].

A match between x[[i]] and y[[j]] is defined as follows:

  • all elements in x[[i]] are contained in y[[j]]

  • all elements in y[[j]] are contained in x[[i]]

For this function, item order and item duplication is ignored.

This function uses logic in the form all(x[[i]] %in% y[[j]]), so it will operate properly with input objects compatible with that format. The function is intended to be used with list that contains atomic vectors.

Examples

x <- list(a=LETTERS[1],
   b=LETTERS[1:2],
   c=LETTERS[2:4]);
x;
#> $a
#> [1] "A"
#> 
#> $b
#> [1] "A" "B"
#> 
#> $c
#> [1] "B" "C" "D"
#> 
y <- list(
   d=LETTERS[1:2],
   e=LETTERS[2],
   f=LETTERS[2:4]);
y;
#> $d
#> [1] "A" "B"
#> 
#> $e
#> [1] "B"
#> 
#> $f
#> [1] "B" "C" "D"
#> 
match_list(x, y)
#>  a  b  c 
#> NA  1  3 
match_list(y, x)
#>  d  e  f 
#>  2 NA  3