[R] nearest neighbour

Rolf Turner r.turner at auckland.ac.nz
Wed Apr 16 03:04:57 CEST 2014

Please keep communications on-list unless there is a genuine reason not to.

To answer your question:  Yes, you need to convert to ppp format; the 
nnwhich() function requires its first argument to be of class "ppp".
My example showed you how to construct or "estimate" a window if you 
don't have or know the window a priori.


Rolf Turner

On 16/04/14 01:23, Monaly Mistry wrote:
> Hi Rolf,
> Thank you very much,  I just have one other question, do I need to
> change the data frame into ppp format?  I've tried using as.ppp but get
> the following error message:
> Error: x,y coords given but no window specified
> Best,
> Monaly.
> On Thu, Apr 3, 2014 at 9:23 PM, Rolf Turner <r.turner at auckland.ac.nz
> <mailto:r.turner at auckland.ac.nz>> wrote:
>     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)

More information about the R-help mailing list