Jim Bouldin writes:

> Can anyone recommend a package that can calculate the distances between

> points (either all or a defined sample of) for x,y point locations listed

> in long format?

The package 'spatstat'  is optimised for this kind of thing.

If you have two vectors x,y containing point coordinates, first convert them to a point pattern using

      Z <- ppp(x, y, range(x), range(y))

Then to get the Euclidean distances between all pairs of points, returned as a matrix of distances, use

    D <- pairdist(X)

To get the distances between all pairs of points that are closer than a distance 'rmax', returned in 'long format', use

    C <- closepairs(X, rmax)

If you take rmax=diameter(as.owin(X)) then all pairs of points will be included.

The resulting object C is a list with entries i, j, xi, yi, xj, yj, dx, dy, d which are respectively

    i = index of point X[i]

    j = index of point X[j]

   xi, yi = coordinates of point X[i]

   xj, yj = coordinates of point X[j]

   dx, dy = vector difference X[j] - X[i]

   d = distance between X[i] and X[j].

You could convert this to a data frame using as.data.frame.

These algorithms are implemented in C and they appear to be at least as fast as anything else available.

There are separate algorithms for finding the nearest neighbour distance (nndist), identifying the nearest neighbour (nnwhich) etc etc.

Adrian Baddeley

	[[alternative HTML version deleted]]

