title: Fastest way to take the weighted sum of the columns of a matrix in R
body: <p>I need the weighted sum of each column of a matrix.</p>
  
  <pre><code>data &lt;- matrix(1:2e7,1e7,2) # warning large number, will eat up &gt;100 megs of memory
  weights &lt;- 1:1e7/1e5
  system.time(colSums(data*weights))
  system.time(apply(data,2,function(x) sum(x*weights)))
  all.equal(colSums(data*weights), apply(data,2,function(x) sum(x*weights)))
  </code></pre>
  
  <p>Typically <code>colSums(data*weights)</code> is faster than the apply call. </p>
  
  <p>I do this operation often (on a large matrix). Hence looking for advice on the most efficient implementation. Ideally, would have been great if we could pass weights to colSums (or rowSums).</p>
  
  <p>Thanks, appreciate any insights!</p>