title: how to calculate all pairwise distances in two dimensions
body: <p>Say I have data concerning the position of animals on a 2d plane (as determined by video monitoring from a camera directly overhead). For example a matrix with 15 rows (1 for each animal) and 2 columns (x position and y position)</p>
  
  <pre><code>animal.ids&lt;-letters[1:15]  
  xpos&lt;-runif(15) # x coordinates 
  ypos&lt;-runif(15) # y coordinates 
  raw.data.t1&lt;-data.frame(xpos, ypos)
    rownames(raw.data.t1) = animal.ids
  </code></pre>
  
  <p>I want to calculate all the pairwise distances between animals. That is, get the distance from animal a (row 1) to the animal in row 2, row3...row15, and then repeat that step for all rows, avoiding redundant distance calculations. The desire output of a function that does this would be the mean of all the pairwise distances. I should clarify that I mean the simple linear distance, from the formula  d&lt;-sqrt(((x1-x2)^2)+((y1-y2)^2)). Any help would be greatly appreciated.</p>
  
  <p>Furthermore, how could this be extended to a similar matrix with an arbitrarily large even number of columns (every two columns representing x and y positions at a given time point). The goal here would be to calculate mean pairwise distances for every two columns and output a table with each time point and its corresponding mean pairwise distance. Here is an example of the data structure with 3 time points:</p>
  
  <pre><code>xpos1&lt;-runif(15) 
  ypos1&lt;-runif(15) 
  xpos2&lt;-runif(15) 
  ypos2&lt;-runif(15)
  xpos3&lt;-runif(15) 
  ypos3&lt;-runif(15)
  pos.data&lt;-cbind(xpos1, ypos1, xpos2, ypos2, xpos3, ypos3)
      rownames(pos.data) = letters[1:15]
  </code></pre>