Last chance! 50% off unlimited learning
Sale ends in
nnwhich(X, ..., method="C")
## 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")
nnwhich.ppp
, the argument X
should be a point
pattern (object of class "ppp"
).
For nnwhich.default
, typically X
andnnwhich.ppp
and nnwhich.default
.k
th nearest neighbour."C"
and "interpreted"
.k
th nearest neighbour).NA
is returned if there is only one point
in the point pattern.k
is specified, the algorithm finds
each point's k
th nearest neighbour. The function nnwhich
is generic, with
a method for point patterns (objects of class "ppp"
)
and a default method.
The method for point patterns 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 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.
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.
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
.
nndist
,
nncross
oldpar <- par(mfrow=c(2,1))
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)
par(oldpar)
Run the code above in your browser using DataLab