title: Multiply many columns by a specific other column in R with data.table?
body: <p>I have a large data.table in R with several columns with dollar values. In a different column I have an inflation adjustment number. I am trying to figure out how to update each of my monetary columns with it multiplied by the inflation adjustment column. Suppose I have the data:</p>
  
  <pre><code>   DT &lt;- data.table(id=1:1000,year=round(runif(1000)*10), 
            inc1 = runif(1000), inc2 = runif(1000), inc3 = runif(1000),    
            deflator = rnorm(1000))
  </code></pre>
  
  <p>which gives output:</p>
  
  <pre><code>             id year      inc1      inc2       inc3    deflator
     1:    1    8 0.4754808 0.6678110 0.41533976 -0.64126988
     2:    2    2 0.6568746 0.7765634 0.70616373  0.39687915
     3:    3    6 0.8192947 0.9236281 0.90002534 -0.69545700
     4:    4    4 0.7781929 0.1624902 0.17565790  0.05263055
     5:    5    7 0.6232520 0.8024975 0.86449836  0.70781887
    ---                                                     
   996:  996    2 0.9676383 0.2238746 0.19822000  0.78564836
   997:  997    9 0.9877410 0.5783748 0.57497438 -1.63365223
   998:  998    8 0.2220570 0.6500632 0.19814932  1.00260174
   999:  999    3 0.4793767 0.2830457 0.54835581  1.04168818
  1000: 1000    8 0.2003476 0.6121637 0.02921505  0.34933690
  </code></pre>
  
  <p>in reality I have <code>inc1</code> - <code>inc100</code>, rather than just three variables and I want to figure out a way to perform this action:</p>
  
  <pre><code>DT[, inc1 := inc1 * deflator]
  </code></pre>
  
  <p>for each of my 100 income columns (inc1, inc2, inc3 in the fake data above).  I will have more than 100 columns in the future, so I would like to figure out a way to loop the action over the columns. Is there a way to do this for all the income columns at once?</p>
  
  <p>I would like to do something like:</p>
  
  <pre><code>inc_cols = c(inc1, inc2, inc3)
  
  DT[, inc_cols := lapply(inc_cols,function(x)= x * deflator),]
  </code></pre>
  
  <p>or</p>
  
  <pre><code>DT[, inc_cols := lapply(.SD,function(x)= x * deflator),.SDcols = inc_cols]
  </code></pre>
  
  <p>but neither of these seem to work. I also tried using the <code>get()</code> function to make it clear <code>deflator</code> is a referencing a column, like:</p>
  
  <pre><code>DT[, inc_cols := lapply(.SD,function(x)= x * get(deflator)),.SDcols = inc_cols]
  </code></pre>
  
  <p>but had no luck. I also tried to loop through the variables with something like:</p>
  
  <pre><code>for (var in inc_cols) {
    print(var)
    DT[, get(var) := get(var) *infAdj2010_mult] 
  }
  </code></pre>
  
  <p>which returns</p>
  
  <pre><code>[1] "inc1"
   Error in get(var) : object 'inc1' not found 
  </code></pre>
  
  <p>I realize this is probably a straight forward question and I have tried to search the other questions here and various online guides and tutorials, but I cannot find an example matching my specific problem. It is similar to this <a href="https://stackoverflow.com/questions/16919998/subtract-every-column-from-each-other-column-in-a-r-data-table" title="link">question</a>, but not exactly. </p>
  
  <p>Thanks for your help!</p>