title: Row sums over columns with a certain pattern in their name
body: <p>I have a data.table like this</p>
  <pre><code>dput(DT)
  structure(list(ref = c(3L, 3L, 3L, 3L), nb = 12:15, i1 = c(3.1e-05, 
  0.044495, 0.82244, 0.322291), i2 = c(0.000183, 0.155732, 0.873416, 
  0.648545), i3 = c(0.000824, 0.533939, 0.838542, 0.990648), i4 = c(0.044495, 
  0.82244, 0.322291, 0.393595)), .Names = c(&quot;ref&quot;, &quot;nb&quot;, &quot;i1&quot;, 
  &quot;i2&quot;, &quot;i3&quot;, &quot;i4&quot;), row.names = c(NA, -4L), class = c(&quot;data.table&quot;, 
  &quot;data.frame&quot;), .internal.selfref = &lt;pointer: 0x0000000000320788&gt;)
  
  DT
  #    ref nb       i1       i2       i3       i4
  # 1:   3 12 0.000031 0.000183 0.000824 0.044495
  # 2:   3 13 0.044495 0.155732 0.533939 0.822440
  # 3:   3 14 0.822440 0.873416 0.838542 0.322291
  # 4:   3 15 0.322291 0.648545 0.990648 0.393595
  </code></pre>
  <p>Now I want to calculate rows sums, but only including columns which start with an &quot;i&quot; (&quot;i1&quot;, &quot;i2&quot;, etc)</p>
  <p>I have used <code>grep</code> to create a vector of the column names to be summed:</p>
  <pre><code>listCol &lt;- colnames(DT)[grep(&quot;i&quot;, colnames(DT))]
  listCol
  # [1] &quot;i1&quot; &quot;i2&quot; &quot;i3&quot; &quot;i4&quot;
  </code></pre>
  <p>Then I have tried to loop over columns:</p>
  <pre><code>DT$sum &lt;- rep.int(0, nrow(DT))
  for (i in listCol){
      DT$sum = DT$sum + DT[ , get(i)]
  }
  </code></pre>
  <p>...which gives the desired output:</p>
  <pre><code>DT
  #    ref nb       i1       i2       i3       i4      sum
  # 1:   3 12 0.000031 0.000183 0.000824 0.044495 0.045533
  # 2:   3 13 0.044495 0.155732 0.533939 0.822440 1.556606
  # 3:   3 14 0.822440 0.873416 0.838542 0.322291 2.856689
  # 4:   3 15 0.322291 0.648545 0.990648 0.393595 2.355079
  </code></pre>
  <p>How can I improve my code?</p>
  <hr />
  <h1>Sub question:</h1>
  <p>This sub-question includes partially the answer to the previous one :</p>
  <p>How to avoid this kind of strange notation :</p>
  <pre><code>myrowMeans = function (x){
      rowMeans(x, na.rm = TRUE)
  }
  DT[ , var := myrowMeans(.SD-myrowMeans(.SD)^2), .SDcols = grep(&quot;i&quot;, colnames(DT))]
  </code></pre>