title: R - subtracting different columns if condition is met
body: <p>I have a huge data frame that is like:</p>
  
  <pre><code>df = data.frame(A = c(1,54,23,2), B=c(1,2,4,65), C=c("+","-","-","+"))
  
  &gt; df
     A  B C
  1  1  1 +
  2 54  2 -
  3 23  4 -
  4  2 65 +
  </code></pre>
  
  <p>I need to subtract the rows based on different conditions, and add these results in a new column:</p>
  
  <pre><code> A - B if C == +
   B - A if C == -
  </code></pre>
  
  <p>So, my output would be:</p>
  
  <pre><code>&gt; new_df
     A  B C   D
  1  1  1 +   0
  2 54  2 - -52
  3 23  4 - -19
  4  2 65 + -63
  </code></pre>