title: Applying a function to a distance matrix in R
body: <p>This question came today in the manipulatr mailing list.</p>
  
  <pre><code>http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f
  </code></pre>
  
  <p>I am rephrasing.</p>
  
  <p>Given a distance matrix (calculated with <code>dist</code>) apply a function to the rows of the distance matrix.</p>
  
  <p>Code:</p>
  
  <pre><code>library(plyr)
  N &lt;- 100
  a &lt;- data.frame(b=1:N,c=runif(N))
  d &lt;- dist(a,diag=T,upper=T)
  sumd &lt;- adply(as.matrix(d),1,sum)
  </code></pre>
  
  <p>The problem is that to apply the function by row you have to store the whole matrix (instead of just the lower triangular part. So it uses too much memory for large matrices. It fails in my computer for matrices of dimensions ~ 10000.</p>
  
  <p>Any ideas?</p>