Finds the nearest neighbour of each point in a point pattern.
nnwhich(X, ...) ## S3 method for class 'ppp': nnwhich(X, \dots, k=1, by=NULL, method="C") ## S3 method for class 'default': nnwhich(X, Y=NULL, \dots, k=1, by=NULL, 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.
- Optional. A factor, which separates
Xinto groups. The algorithm will find the nearest neighbour in each group.
- 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 the argument
by is given, it should be a
of length equal to the number of points in
This factor effectively partitions
X into subsets,
each subset associated with one of the levels of
The algorithm will then find, for each point of
the nearest neighbour in each subset.
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.
If the argument
byis given, then the result is a data frame containing the indices described above, from each point of
X, to the nearest point in each subset of
Xdefined by the factor
Nearest neighbours of each type
X is a multitype point pattern
by=marks(X), then the algorithm will find,
for each point of
X, the nearest neighbour
of each type. See the Examples.
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) # nearest neighbours of each type head(nnwhich(ants, by=marks(ants)))