# [R] Encircle the closest points

Rolf Turner rolf.turner at xtra.co.nz
Thu Apr 11 12:00:19 CEST 2013

You might wish to use the nndist() and nnwhich() functions from
the spatstat package.  E.g., using your data,

Y <-  structure(c(2372L, 2494L, 2170L, 2238L, 1918L, 715L, 1573L, 2437L,
2481L, 1881L, 440L, 2165L, 343L, 121L, 597L, 1163L, 1459L, 2093L, 1454L,
1761L, 1872L, 174L, 2040L, 552L, 1403L, 6381L, 1649L, 4136L, 4415L,
10650L, 12111L, 4397L, 9593L, 7040L, 3888L, 8340L, 11475L, 11413L,
3985L, 1077L, 7723L, 1392L, 4099L, 4386L, 4737L, 1731L, 9246L, 7729L,
4264L, 6880L), .Dim = c(25L, 2L))
Y <- as.ppp(Y,W=ripras(Y,shape="rectangle"))
nnd <- nndist(Y,k=1:3)
nnw <- nnwhich(Y,k=1:3)

You will see, for instance, that points 9, 25, and 23 are the three
points nearest to point 1, and that they are at distances 667.95359,
1089.9367 and 1388.2824 respectively.

HTH

cheers,

Rolf Turner

On 04/11/2013 07:48 PM, eliza botto wrote:
> Dear R-user,
> For the "matplot" of the following data drawn by
>> matplot(t, pch=1)
> structure(c(2372L, 2494L, 2170L, 2238L, 1918L, 715L, 1573L, 2437L, 2481L, 1881L, 440L, 2165L, 343L, 121L, 597L, 1163L, 1459L, 2093L, 1454L, 1761L, 1872L, 174L, 2040L, 552L, 1403L, 6381L, 1649L, 4136L, 4415L, 10650L, 12111L, 4397L, 9593L, 7040L, 3888L, 8340L, 11475L, 11413L, 3985L, 1077L, 7723L, 1392L, 4099L, 4386L, 4737L, 1731L, 9246L, 7729L, 4264L, 6880L), .Dim = c(25L, 2L))
>
> How can i encircle (locate), on the plotting window, the closest 3 point around any selected point. More precisely, for x=2372  and y=6381, which are the 3 closest points?