title: Loop through rows dataframe in r and check for if else function statement
body: <p>A reproducible example:</p>
  <pre><code>example &lt;- structure(list(seqnames = c(&quot;chr1&quot;, &quot;chr1&quot;, &quot;chr1&quot;, &quot;chr1&quot;, &quot;chr1&quot;, 
  &quot;chr1&quot;), start = c(14660L, 661861L, 662360L, 700216L, 703359L, 
  713320L), end = c(14736L, 661929L, 662414L, 700326L, 703430L, 
  713395L), width = c(77L, 69L, 55L, 111L, 72L, 76L), strand = c(&quot;+&quot;, 
  &quot;+&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;)), row.names = c(NA, -6L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>That looks like that:</p>
  <pre><code>  seqnames  start    end width strand
  1     chr1  14660  14736    77      +
  2     chr1 661861 661929    69      +
  3     chr1 662360 662414    55      -
  4     chr1 700216 700326   111      -
  5     chr1 703359 703430    72      -
  6     chr1 713320 713395    76      -
  </code></pre>
  <p>My function looks for <code>start</code> and adds <code>100</code> if start has as <code>strand</code> the <code>+</code> sign, for the <code>-</code> strand the <code>end</code> is diminished by <code>100</code>.</p>
  <pre><code>extension &lt;- function(peak_df) {
    if(peak_df['strand']=='+'){
      peak_df['end'] = peak_df['start'] + 100
    }
    else if (peak_df['strand']=='-') {
      peak_df['start'] = peak_df['end'] - 100
    }
  }
  </code></pre>
  <p>Then I want to take this function and loop trough the rows of <code>example</code>. If I use <code>apply</code> I get the following error:</p>
  <pre><code>&gt; apply(pk.df,1, extension)
  Error in peak_df[&quot;end&quot;] - 100 : non-numeric argument to binary operator
  </code></pre>
  <p>If I use <code>sapply</code> then I get a different error:</p>
  <pre><code>&gt; sapply(pk.df,extension)
  Error in if (peak_df[&quot;strand&quot;] == &quot;+&quot;) { : 
    missing value where TRUE/FALSE needed
  </code></pre>
  <p>Is this because my function is not vectorized ?</p>