title: How to select and remove specific elements or find their index in a vector or matrix?
body: <p>Let's say I have two vectors:</p>
  
  <pre><code>x &lt;- c(1,16,20,7,2)
  
  y &lt;- c(1, 7, 5,2,4,16,20,10)
  </code></pre>
  
  <p>I want to remove elements in <code>y</code> that are not in <code>x</code>. That is, I want to remove elements <code>5, 4, 10</code> from <code>y</code>.</p>
  
  <pre><code>y
  [1] 1 7 2 16 20 
  </code></pre>
  
  <p>In the end, I want vectors <code>x</code> and <code>y</code> to have to same elements. Order does not matter.</p>
  
  <p>My thoughts: The <code>match</code> function lists the indices of the where the two vectors contains a matching element but I need a function is that essentially the opposite. I need a function that displays the indices where the elements in the two vectors don't match.</p>
  
  <pre><code># this lists the indices in y that match the elements in x
  match(x,y)
  [1] 1 6 7 2 4   # these are the indices that I want; I want to remove
                  # the other indices from y
  </code></pre>
  
  <p>Does anyone know how to do this? thank you</p>