# [R-sig-Geo] nearest neighbor 3d point pattern

Rolf Turner r.turner at auckland.ac.nz
Fri Jun 28 01:34:39 CEST 2013

```On 27/06/13 23:40, Carsten Neumann wrote:
> Dear list members,
>
> after searching R mailing lists, I wonder if there is a R function that
> can find the nearest neighbor/or just euclidean distance of points in
> one 3D coordinate system to points in another 3D coordinate system. The
> function nncross {spatstat} can only deal with 2D point pattern. Any
> idea or experiences with such procedures?
>
> ######################Test Data############################
> point1<-array(c(seq(0,1,0.1),seq(0,1,0.1),seq(0,1,0.1)),dim=c(11,11,11))
> point2<-array(c(0.8,0.3,0.5),dim=c(1,1,1))
> #########################################################
> question: where is point2 in point1?

You are correct in that nncross() has not (yet) been extended to three
dimensional point patterns.  However a workaround can be found by
using crossdist.pp3().

E.g.:

set.seed(42)
X     <- ppx(data.frame(x=rnorm(20),y=rnorm(20),z=rnorm(20)))
Y      <- ppx(data.frame(x=0,y=0,z=0))
M     <- crossdist(Y,X)
min(M)
which.min(M)

This will tell you that the nearest point in X to c(0,0,0) is point
number 15
and the distance from c(0,0,0) to this point is 0.5299162.

I'm afraid I could not comprehend your example.  The object point1 is
a 3-dimensional array (11 x 11 x 11).  How is this to be interpreted as
a 3-dimensional *point pattern*?  And point2 is a single number --- 0.8,
and not actually a point in 3-space.

Furthermore what does the question "where is point2 in point1?" mean?
I cannot parse this question.

Perhaps (quite possibly) I am just being stupid.  If so, please
enlighten me.

cheers,

Rolf Turner

```