title: Joining tables with identical (non-keyed) column names in R data.table
body: <p>How do you deal with identically named, non-key columns when joining data.tables? I am looking for a solution to <code>table.field</code> notation in SQL.</p>
  
  <p>For instance, lets' say I have a table DT that is repopulated with new data for column <code>v</code>  every time period. I also have a table DT_HIST that stores entries from previous time periods (<code>t</code>). I want to find the difference between the current and previous time period for each <code>x</code></p>
  
  <p>In this case: DT is time period 3, and DT_HIST has time periods 1 and 2:</p>
  
  <pre><code> DT &lt;- data.table(x=c(1,2,3,4),v=c(20,20,35,30))
   setkey(DT,x)
   DT_HIST &lt;- data.table(x=rep(seq(1,4,1),2),v=c(40,40,40,40,30,25,45,40),t=c(rep(1,4),rep(2,4)))
   setkey(DT_HIST,x)
  
  &gt; DT
     x  v
  1: 1 20
  2: 2 20
  3: 3 35
  4: 4 30
  
  &gt; DT_HIST
     x  v t
  1: 1 40 1
  2: 1 30 2
  3: 2 40 1
  4: 2 25 2
  5: 3 40 1
  6: 3 45 2
  7: 4 40 1
  8: 4 40 2
  </code></pre>
  
  <p>I would like to join <code>DT</code> with <code>DT_HIST[t==1,]</code> on <code>x</code> and calculate the difference in <code>v</code>.</p>
  
  <p>Just joining the tables results in columns <code>v</code> and <code>v.1</code>. </p>
  
  <pre><code>&gt; DT[DT_HIST[t==2],]
     x  v v.1 t
  1: 1 20  30 2
  2: 2 20  25 2
  3: 3 35  45 2
  4: 4 30  40 2
  </code></pre>
  
  <p>However, I can't find a way to refer to the different <code>v</code> columns when doing the join.</p>
  
  <pre><code>&gt; DT[DT_HIST[t==2],list(delta=v-v.1)]
  Error in `[.data.table`(DT, DT_HIST[t == 2], list(delta = v - v.1)) : 
  object 'v.1' not found
  
  &gt; DT[DT_HIST[t==2],list(delta=v-v)]
     x delta
  1: 1     0
  2: 2     0
  3: 3     0
  4: 4     0
  </code></pre>
  
  <p>If this is a duplicate, I apologize. I searched and couldn't find a similar question.</p>
  
  <p>Also, I realize that I can simply rename the columns after joining and then run my desired expression, but I want to know if I'm doing this in the completely wrong way.</p>