title: Aggregate rows in a large matrix by rowname
body: <p>I would like to aggregate the rows of a matrix by adding the values in rows that have the same rowname. My current approach is as follows:</p>
  
  <pre><code>&gt; M
    a b c d
  1 1 1 2 0
  1 2 3 4 2
  2 3 0 1 2
  3 4 2 5 2
  &gt; index &lt;- as.numeric(rownames(M))
  &gt; M &lt;- cbind(M,index)
  &gt; Dfmat &lt;- data.frame(M)
  &gt; Dfmat &lt;- aggregate(. ~ index, data = Dfmat, sum)
  &gt; M &lt;- as.matrix(Dfmat)
  &gt; rownames(M) &lt;- M[,"index"]
  &gt; M &lt;- subset(M, select= -index)
  &gt; M
     a b c d
   1 3 4 6 2
   2 3 0 1 2
   3 4 2 5 2
  </code></pre>
  
  <p>The problem of this appraoch is that i need to apply it to a number of very large matrices (up to 1.000 rows and 30.000 columns). In these cases the computation time is very high (Same problem when using ddply). Is there a more eficcient to come up with the solution? Does it help that the original input matrices are DocumentTermMatrix from the tm package? As far as I know they are stored in a sparse matrix format.</p>