For each point in the given point pattern, this function finds
its nearest neighbour (the nearest other point of the pattern).
By default it returns a vector giving, for each point,
the index of the point's
nearest neighbour. If k is specified, the algorithm finds
each point's kth nearest neighbour. The function nnwhich is generic, with
method for point patterns (objects of class "ppp")
and a default method which are described here, as well as a method for
three-dimensional point patterns (objects of class "pp3",
described in nnwhich.pp3.
The method nnwhich.ppp expects a single
point pattern argument X.
The default method expects that X and Y will determine
the coordinates of a set of points. Typically X and
Y would be numeric vectors of equal length. Alternatively
Y may be omitted and X may be a list with two components
named x and y, or a matrix or data frame with two columns.
The argument k may be a single integer, or an integer vector.
If it is a vector, then the $k$th nearest neighbour distances are
computed for each value of $k$ specified in the vector.
If there are no points (if x has length zero)
a numeric vector of length zero is returned.
If there is only one point (if x has length 1),
then the nearest neighbour is undefined, and a value of NA
is returned. In general if the number of points is less than or equal
to k, then a vector of NA's is returned.
The argument method is not normally used. It is
retained only for checking the validity of the software.
If method = "interpreted" then the distances are
computed using interpreted R code only. If method="C"
(the default) then C code is used.
The C code is faster by two to three orders of magnitude
and uses much less memory.
To evaluate the distance between a point and its nearest
neighbour, use nndist.
To find the nearest neighbours from one point pattern
to another point pattern, use nncross.