Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding indices of all matches between two lists

Tags:

list

matching

r

I need to take subsets from a list of documents based on a random sample. I know there are a lot of questions about matching on stackoverflow, but I didn't see one that addressed my question.

To demonstrate what I am looking for, suppose we have two vectors:

> A <- c(1,2,4,5,8,9)
> B <- c(1,3,2,2,3,4,5,6,7,7,8,9,10)

The function I need is similar to doing

> 1 == B
> [1] T F F F F F F F F F F F F

That is, I need a function f such that

> f(A,B)
> [1] T F T T F T T F F F T T F

Any ideas? match(B,A) returns something similar to what I'm looking for, but not quite.

like image 462
Daniel Watkins Avatar asked Sep 11 '25 11:09

Daniel Watkins


1 Answers

It's as simple as B %in% A...

like image 97
baptiste Avatar answered Sep 13 '25 01:09

baptiste