title: keep only the last word in the sequence
body: <p>I want to keep only the last word in the sequence. Also, there are specific words that I want to keep and don't remove if it appears in the sequence, and it should go the first of the sequence.  </p>
  
  <p>Let say I have two dictionaries of words.</p>
  
  <pre><code>myDictionary&lt;-c("A","B","C","p")
  Priority_dic&lt;-c("p") 
  </code></pre>
  
  <p>here, Proprity_dic shows the words that I want to keep if it appears wherever in the sequence.  </p>
  
  <p>I want to remove all words (in myDictionary) in the sequence and keep only the last word. Also, if Priority_dic appear in the sequence, put it in the first.</p>
  
  <p>for example
  I have a sequence </p>
  
  <pre class="lang-r prettyprint-override"><code>df&lt;-read.table(text="
  id  mySequece
  1   AAAB
  2   ABABC
  3   ApBpA
  4   AACBBCp
  5   ppp",header=T,stringsAsFactors = F)
  </code></pre>
  
  <p>result</p>
  
  <pre><code>id  mySequece
  1   B
  2   C
  3   pA
  4   pC
  5   p
  </code></pre>