-------------------------------------------------------------------------------
help for nearest
-------------------------------------------------------------------------------

Distance and identity of nearest neighbour from point coordinates

nearest xvar yvar [if exp] [in range] , dist(distvar) [id(idvar)]

Description

nearest calculates the distance from each point to its nearest neighbour given the planar point coordinates given in xvar and yvar. Distance is calculated as straight-line (crow-fly) Euclidean or Pythagorean distance. Optionally, the identity (observation number in current sort order) of the nearest neighbour may also be generated.

Remarks

British English neighbour corresponds to American English neighbor.

Options dist(distvar) specifies a new variable for holding the distances. This is a required option.

id(idvar) specifies a new variable for holding the identifiers (observation numbers in current sort order) of the nearest neighbours. If two or more points are equally distant from any given point, that with the lowest observation number is regarded as the nearest neighbour.

Examples

. nearest x y, id(id) dist(dist)

Author

Nicholas J. Cox, University of Durham, U.K. n.j.cox@durham.ac.uk Acknowledgements

This problem was suggested by Benoit Dulong.