title: Keep unique ranked values from 2 character vectors in R
body: <p>I have two character vectors of length 5:</p>
  
  <pre class="lang-r prettyprint-override"><code>x &lt;- c("B","A","D","K","F")
  y &lt;- c("A","C","D","F","E")
  </code></pre>
  
  <p>The characteristics of these vectors are the following:</p>
  
  <ol>
  <li>Each vector is ordered (for example, in vector <code>x</code> element <code>"B"</code> is more important than <code>"A"</code>)</li>
  <li>There are no duplicates inside each vector, but there <strong>may</strong> be duplicates amongst them (as it's the case in this example)</li>
  </ol>
  
  <p>What I want is to "merge"  these 2 vectors in order to obtain a new vector <code>z</code> <strong>of length 5</strong> which contains the most important <strong>non duplicate</strong> elements of vectors <code>x</code> and <code>y</code>, respectively, giving a priority to <code>x</code> in case of ties.<br>
  To be clearer, the logic of my program should work as follows:</p>
  
  <ol>
  <li>Take the first element of <code>x</code> and <code>y</code>: if they're different, put them inside <code>z</code></li>
  <li>Take the second element of <code>x</code> and <code>y</code>: if there's a value that is already inside <code>z</code>, ignore it. Else, put it in.<br>
  Another way to see this is: if there's a value which is stored in an higher position either in <code>x</code> or <code>y</code>, ignore it. Else, put it inside <code>z</code>.</li>
  <li>Go to the next position and perform 2 again, comparing each value to all the values in higher positions both in <code>x</code> and <code>y</code>.</li>
  <li>Stop when <code>length(z)==5</code></li>
  </ol>
  
  <p>In my example it would work as follows:</p>
  
  <ol>
  <li>Put <code>B</code> and <code>A</code> inside <code>z</code></li>
  <li>Put <code>C</code> inside <code>z</code></li>
  <li>Put <code>D</code> inside <code>z</code> (which is now of length 4)</li>
  <li>We have now values <code>K</code> and <code>F</code>: if we put both, <code>length(z)</code> will be 6, so take just <code>K</code> as it's inside <code>x</code> (priority)</li>
  </ol>
  
  <p>The resulting vector would be <code>z = c("B","A","C","D","K")</code>.  </p>
  
  <p>I could transform the vectors in lists or whatever, but I would like to avoid using loops.</p>