title: Subtraction to combine two R data.tables by matching rows
body: <p>I would like to subtract one R data.table from another. I have a list of matching columns, and a list of columns for the subtraction to operate on.</p>
  
  <pre><code>dt1 &lt;- data.table(read.table(header=T, text=
  "Date           Code       ColumnA    ColumnB Session
  01/01/2013      AB         0          5       PRE
  01/01/2013      CJ         15         25      PRE
  01/01/2013      JJ         20         20      PRE
  02/01/2013      JJ         25         15      PRE"))
  
  dt2 &lt;- data.table(read.table(header=T, text=
  "Date           Code      ColumnA    ColumnB Session
  01/01/2013      BB        15         25      POST
  01/01/2013      AB        1          2       POST
  02/01/2013      AB        25         15      POST
  02/01/2013      JJ        35         15      POST"))
  
  matchingCols &lt;- c("Date", "Code")
  mergingCols &lt;- names(dt1)[3:4]
  </code></pre>
  
  <p>I would like to get the following data.table:</p>
  
  <pre><code>         Date Time ColumnA ColumnB Session
  1: 01/01/2013   AB      -1       3 PREPOST
  2: 02/01/2013   JJ     -10       0 PREPOST
  </code></pre>
  
  <p>The mergingCols values in dt2 should be subtracted from dt1 for rows in which matchingCols are the same so I can obtain the difference values. There are too many mergingCols to specify them individually in the code.</p>
  
  <p>Here are similar questions, but I could not adapt them for subtraction:</p>
  
  <p><a href="https://stackoverflow.com/questions/16846380/how-to-apply-same-function-to-every-specified-column-in-a-data-table">How to apply same function to every specified column in a data.table</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14731861/merge-data-frames-whilst-summing-common-columns-in-r">Merge data frames whilst summing common columns in R</a></p>
  
  <p><a href="https://stackoverflow.com/questions/42942405/combine-data-tables-and-sum-the-shared-column">combine data.tables and sum the shared column</a></p>
  
  <p>Would either an rbind then aggregate approach or a joining approach work?</p>