[R] nearest neighbour

Rolf Turner r.turner at auckland.ac.nz
Thu Apr 3 22:23:29 CEST 2014


On 04/04/14 01:16, Monaly Mistry wrote:
> Hi,
>
> If I have a data frame of the location of individuals (x and y coordinate),
> how do I find the 5 nearest neighbours for each individual.

The nnwhich() function from the spatstat package will do this for you.

E.g.:

require(spatstat) # You need to have *installed* spatstat!
W <- ripras(ddd)  # Where "ddd" is the name of your data frame.
X <- ppp(x=ddd[,1],y=ddd[,2],window=W)
n5 <- nnwhich(X,k=1:5)

cheers,

Rolf Turner




More information about the R-help mailing list