title: R: matching data.table columns as function parameters for a function applied to another data.table
body: <p>I'm trying to scale some data for plotting.  Some of the scales might be provided by the user, and others can be derived from the data directly.</p>
  
  <p>So a sample set of data might be:</p>
  
  <pre><code>dt1 &lt;- data.table(Time=1:10, a=10:1, b=11:20, c=rep(11:15,2))
  setkey(dt1,"Time")
  </code></pre>
  
  <p>And the ranges for plotting might be:</p>
  
  <pre><code>dt.ranges &lt;- data.table(a=c(1,10), b=c(11,20), c=c(11,20))
  </code></pre>
  
  <p>The idea is for each <code>dt1</code> column, to scale the column by the matching 
  name in <code>dt.ranges</code>.</p>
  
  <p>I can do this using old-school data.frame style programming, but I'm wondering whether there's a quicker / more elegant data.table-oriented approach.</p>
  
  <p>The clunky method that gives the correct answer is:</p>
  
  <pre><code>dt2 &lt;- data.table(
    data.frame(
      lapply(names(dt1)[names(dt1) %in% names(dt.ranges)],
             function(nn,rt){
               print(nn)
               (dt1[[nn]] - min(rt[[nn]]))/(max(rt[[nn]]) - min(rt[[nn]]))
             }, 
             dt.ranges)
    )
  )
  names(dt2) &lt;- names(dt1)[-1]
  dt2$Time &lt;- dt1$Time
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>            a         b         c Time
   1: 1.0000000 0.0000000 0.0000000    1
   2: 0.8888889 0.1111111 0.1111111    2
   3: 0.7777778 0.2222222 0.2222222    3
   4: 0.6666667 0.3333333 0.3333333    4
   5: 0.5555556 0.4444444 0.4444444    5
   6: 0.4444444 0.5555556 0.0000000    6
   7: 0.3333333 0.6666667 0.1111111    7
   8: 0.2222222 0.7777778 0.2222222    8
   9: 0.1111111 0.8888889 0.3333333    9
  10: 0.0000000 1.0000000 0.4444444   10
  </code></pre>
  
  <p>Does anyone know a better way?</p>
  
  <p>Thanks</p>
  
  <p>Jason</p>