Finds the nearest neighbour of each point in a point pattern.
nnwhich(X, ...) ## S3 method for class 'ppp': nnwhich(X, \dots, k=1, method="C") ## S3 method for class 'default': nnwhich(X, Y=NULL, \dots, k=1, method="C")
- Arguments specifying the locations of
a set of points.
nnwhich.ppp, the argument
Xshould be a point pattern (object of class
- Ignored by
- Integer, or integer vector. The algorithm will compute the distance to the
kth nearest neighbour.
- String specifying which method of calculation to use.
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
kth nearest neighbour.
nnwhich is generic, with
method for point patterns (objects of class
and a default method which are described here, as well as a method for
three-dimensional point patterns (objects of class
nnwhich.ppp expects a single
point pattern argument
The default method expects that
Y will determine
the coordinates of a set of points. Typically
Y would be numeric vectors of equal length. Alternatively
Y may be omitted and
X may be a list with two components
y, or a matrix or data frame with two columns.
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
is returned. In general if the number of points is less than or equal
k, then a vector of
NA's is returned.
method is not normally used. It is
retained only for checking the validity of the software.
method = "interpreted" then the distances are
computed using interpreted R code only. If
(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
To find the nearest neighbours from one point pattern
to another point pattern, use
- Numeric vector or matrix giving, for each point,
the index of its nearest neighbour (or
kth nearest neighbour).
k = 1(the default), the return value is a numeric vector
vgiving the indices of the nearest neighbours (the nearest neighbout of the
ith point is the
jth point where
j = v[i]). If
kis a single integer, then the return value is a numeric vector giving the indices of the
kth nearest neighbours.
kis a vector, then the return value is a matrix
m[i,j]is the index of the
k[j]th nearest neighbour for the
ith data point.
A value of
NA is returned if there is only one point
in the point pattern.
data(cells) plot(cells) m <- nnwhich(cells) m2 <- nnwhich(cells, k=2) # plot nearest neighbour links b <- cells[m] arrows(cells$x, cells$y, b$x, b$y, angle=15, length=0.15, col="red") # find points which are the neighbour of their neighbour self <- (m[m] == seq(m)) # plot them A <- cells[self] B <- cells[m[self]] plot(cells) segments(A$x, A$y, B$x, B$y)