[R] Distances between two datasets of x and y co-ordinates

adrian at maths.uwa.edu.au adrian at maths.uwa.edu.au
Fri Mar 14 04:52:42 CET 2008


Andrew McFadden <Andrew.McFadden at maf.govt.nz> writes:

> I am trying to determine the distances between two datasets of x and y
> points.

This can be done efficiently in the package 'spatstat'.

     library(spatstat)
     crossdist(x1, y1, x2, y2)

where x1, y1 are vectors of coordinates for the first set of points
and x2, y2 for the second set. See help(crossdist.default)

This is executed in C and is faster than using outer() or apply().

The result is a matrix giving the distance between each pair of points
(the first point in the first dataset and the second point in the second
set). If these datasets are large, you can of course run into trouble with
the size of this matrix.

If you just wanted to find the distance to the *nearest* point
(or identify which point is nearest), use the function nncross().

Adrian Baddeley



More information about the R-help mailing list