title: Using data.table to calculate a function which depends on many columns
body: <p>There are many posts which discuss applying a function over many columns when using data.table.  However I need to calculate a function which depends on many columns.  As an example:</p>
  
  <pre><code># Create a data table with 26 columns.  Variable names are var1, ..., var 26
  data.mat = matrix(sample(letters, 26*26, replace=TRUE),ncol=26)
  colnames(data.mat) = paste("var",1:26,sep="")
  data.dt &lt;- data.table(data.mat)
  </code></pre>
  
  <p>Now, say I would like to count the number of 'a's in columns 5,6,7  and 8. 
  I cannot see how to do this with SDcols and end up doing:</p>
  
  <pre><code>data.dt[,numberOfAs := (var5=='a')+(var6=='a')+(var7=='a')+(var7=='a')]
  </code></pre>
  
  <p>Which is very tedious.  Is there a more sensible way to do this?</p>
  
  <p>Thanks</p>