title: R, compare strings and count
body: <p>I have a column with short sequences. I am trying to count gaps (&quot;-&quot;) in ref column with respect to the ip.</p>
  <p>here is the data frame</p>
  <pre><code>df &lt;- structure(list(ip = c(&quot;ATCGGGTTA&quot;, &quot;AT--GATCT&quot;, &quot;AT-GGATCT&quot;), 
      ref = c(&quot;AT--GATCT&quot;, &quot;ATCGGGTTA&quot;, &quot;AT--GATCT&quot;), gap = c(2L, 
      0L, 1L)), row.names = c(NA, -3L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>logic -&gt; if ip == &quot;A|C|T|G&quot;, and ref == &quot;-&quot; then gap</p>
  <p>I tried following code:</p>
  <pre><code>mapply(function(x,y) if(x != '-' ) sum(y==&quot;-&quot;),strsplit(df$ip,&quot;&quot;),strsplit(df$ref,&quot;&quot;))
  
  [1] 2 0 2
  </code></pre>
  <p>for the 3rd row, it doesn't return 1. It should only count when there is an alphabet in IP and a gap in ref at the same position.</p>
  <p>Thanks!</p>