title: Detect partial string matches in R
body: <p>I am trying to count the number of transactions which start with <em>AK</em> and also contain <em>AK</em> within the transaction, but don't end in <em>AK</em>.</p>
  
  <p>Examples:</p>
  
  <p><em><strong>EXCLUDE</em>:
  example</strong>: AK->se (no AK in between)</p>
  
  <p><em><strong>EXCLUDE</em></strong>: AK->gg->se->ll : does not include <em>AK</em> within the transaction</p>
  
  <p><em><strong>INCLUDE:</em>
  example:</strong>
  AK->se->Ak->gg</p>
  
  <p><strong>Sample data:</strong></p>
  
  <pre><code>f&lt;- data.frame(
  id=c("A","A","A","A","C","C","D","D","E"),
  Mode=c("AK-&gt;se","se-&gt;AK-&gt;gg, bishan-&gt;K","AK-&gt;se","se-&gt;gr-&gt;gg, bishan-&gt;AK","AK-&gt;se","se-&gt;gr-&gt;gg, bishan-&gt;AK","AK-&gt;se","se-&gt;gr-&gt;gg, bishan-&gt;AK","se-&gt;AK-&gt;df, hg-&gt;pp-&gt;sk")
  )
  </code></pre>
  
  <p><em>I need to deal with a large amount of data so optimization is crucial.</em></p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Edited</strong></p>
  
  <pre><code>f&lt;- data.frame(
  id=c("A","A","A","A","C","C","D","D","E"),
  Mode=c("AK-&gt;se","se-&gt;AK-&gt;gg, bishan-&gt;K","AK-&gt;se","se-&gt;gr-&gt;gg, bishan-&gt;AK","AK-&gt;se","AK-&gt;AK-&gt;gg, bishan-&gt;AK","AK-&gt;se-&gt;Ak-&gt;gg","se-&gt;gr-&gt;gg, bishan-&gt;AK","AK-&gt;AK-&gt;df, hg-&gt;pp-&gt;sk")
  )
  </code></pre>