title: Summarize dataframe with a constraint using Dplyr
body: <p>I have a dataframe similar to the one below:</p>
  
  <pre><code>data &lt;- data.frame(x = c("0", "2", "8", "1", "7", "10", "15", "14", "13", "11"),
                     y = c("11", "5", "14", "9", "13", "7", "4", "0", "12", "8"),
                     act_x = c("Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy"),
                     act_y = c("Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short"))
  </code></pre>
  
  <p>I wish to create a profits column for x and a profits column for y based on the action taken for x and y. The result should look something like this:</p>
  
  <pre><code>res &lt;- data.frame(data,
                    prof_x = c(NA, -2, 6, 7, 6, -3, 5, 1, -1, 2),
                    prof_y = c(NA, -6, -9, -5, -4, -6, 3, -4, -12, -4))
  </code></pre>
  
  <p>For example, starting at day 0 (the first line), I short x and buy y. The corresponding prices move and settle at day 1 (the second line). The profit of x is <code>0-2=-2</code> (since I shorted x) and the profit for y is <code>5-11=-6</code> (since I bought y). And so on...</p>
  
  <p>Is there a friendly way to implement this in the Dplyr pipe? Does anyone have any suggestions outside of the pipe? Thanks in advance for any guidance. </p>