title: R perform calculations on groups for subset of rows
body: <p>In other words, I want to group on a column and then perform calculations using only some of the rows per group.</p>
  
  <p>The data set I have is:</p>
  
  <pre><code>    LoanRefId               Tran_Type TransactionAmount
  103        11               LoanIssue         1000.0000
  104        11           InitiationFee          171.0000
  105        11                Interest           59.6729
  106        11       AdministrationFee           64.9332
  107        11 RaisedClientInstallment         1295.5757
  108        11       ClientInstallment         1295.4700
  109        11                  PaidUp            0.0000
  110        11              Adjustment            0.1361
  111        11                  PaidUp            0.0000
  112        12               LoanIssue         3000.0000
  113        12           InitiationFee          399.0000
  114        12                Interest           94.9858
  115        12       AdministrationFee           38.6975
  116        12 RaisedClientInstallment         3532.6350
  117        12       ClientInstallment         3532.6100
  118        12                  PaidUp            0.0000
  119        12              Adjustment            0.0733
  120        12                  PaidUp            0.0000
  </code></pre>
  
  <p>I would like to repeat the following calculation for each group:
  <code>ClientInstallment</code> - <code>LoanIssue</code>.</p>
  
  <p>So, group 1 will be for <code>LoanRefId</code> number 11. The calculation will take <code>ClientInstallment</code> of 1295.47 and subtract <code>LoanIssue</code> of 1000 to give me a new column, call it "Income, with value 295.47.</p>
  
  <p>Is this possible using <code>data.table</code> or <code>dplyr</code> or any other clever tricks.</p>
  
  <p>Alternatively I can create two data summaries, one for <code>Clientinstallment</code> and one for <code>LoanIssue</code> and then subtract them, but the truth is I need to do much more than just subtracting two numbers, so I would need a data summary for each calculation which is just plain unclever imho.</p>
  
  <p>any help is appreciated</p>