title: Changing multiple Columns in data.table r
body: <p>I am looking for a way to manipulate multiple columns in a data.table in R. As I have to address the columns dynamically as well as a second input, I wasn't able to find an answer.</p>
  
  <p>The idea is to index two or more series on a certain date by dividing all values by the value of the date eg:</p>
  
  <pre><code>set.seed(132)
  # simulate some data
  dt &lt;- data.table(date = seq(from = as.Date("2000-01-01"), by = "days", length.out = 10),
                   X1 = cumsum(rnorm(10)),
                   X2 = cumsum(rnorm(10)))
  
  # set a date for the index
  indexDate &lt;- as.Date("2000-01-05")
  
  # get the column names to be able to select the columns dynamically
  cols &lt;- colnames(dt)
  cols &lt;- cols[substr(cols, 1, 1) == "X"]
  </code></pre>
  
  <p><strong>Part 1: The Easy data.frame/apply approach</strong></p>
  
  <pre><code>df &lt;- as.data.frame(dt)
  # get the right rownumber for the indexDate
  rownum &lt;- max((1:nrow(df))*(df$date==indexDate))
  
  # use apply to iterate over all columns
  df[, cols] &lt;- apply(df[, cols], 
                      2, 
                      function(x, i){x / x[i]}, i = rownum)
  </code></pre>
  
  <p><strong>Part 2: The (fast) data.table approach</strong>
  So far my data.table approach looks like this:</p>
  
  <pre><code>for(nam in cols) {
    div &lt;- as.numeric(dt[rownum, nam, with = FALSE])
    dt[ , 
       nam := dt[,nam, with = FALSE] / div,
       with=FALSE]
  }
  </code></pre>
  
  <p>especially all the <code>with = FALSE</code> look not very data.table-like.</p>
  
  <p>Do you know any faster/more elegant way to perform this operation?</p>
  
  <p>Any idea is greatly appreciated!</p>