title: Is there a way in R to create a matrix of nearest neighbours and variable values inR?
body: <p>I have data which looks like this:</p>
  <pre><code>   identity  growth x-pos y-pos
  1:     Z      0.1   0.5   0.7
  2:     B      0.1   0.1   0.0
  3:     C      0.2   4.6   2.5
  4:     D      0.3   5.6   5.0
  5:     A      0.4   0.2   1.0
  6:     P      0.1   0.4   2.0
  </code></pre>
  <p>I would like to compare if growth values are correlated between n nearest neighbours for each object with a unique identity.
  So basically create a matrix which identifies the 5 nearest neighbours for each unique <code>identity</code> row based on the locations denoted by <code>x-pos</code> and <code>y-pos</code> and perform corelations between the <code>growth</code> value of object (e.g. <code>Z</code>) and the growth value of the 1st, 2nd, 3rd, 4th and 5th nearest neighbour of <code>Z</code>.</p>
  <p>I tried making a euclidian matrix and then using a measure of autocorrelation using the ADE package but was wondering is there is an simpler way to construct such a matrix.</p>