Finds the nearest neighbour of each point in a point pattern.
nnwhich(X, ..., method="C") ## S3 method for class 'ppp': nnwhich(X, \dots, method="C") ## S3 method for class 'default': nnwhich(X, Y=NULL, \dots, 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
- 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). It returns a vector giving, for each point, the index of the point's nearest neghbour.
nnwhich is generic, with
a method for point patterns (objects of class
and a default method.
The method for point patterns 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.
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.
If there is only one point (if
x has length 1),
then the nearest neighbour is undefined, and a value of
If there are no points (if
x has length zero)
a numeric vector of length zero is returned.
To evaluate the distance between a point and its nearest
To find the nearest neighbours from one point pattern
to another point pattern, use
- Integer vector giving, for each point, the index of its nearest neighour.
A value of
NA is returned if there is only one point
in the point pattern.
oldpar <- par(mfrow=c(2,1)) data(cells) plot(cells) m <- nnwhich(cells) # 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) par(oldpar)