# nnwhich

##### Nearest neighbour

Finds the nearest neighbour of each point in a point pattern.

##### Usage

```
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")
```

##### Arguments

- X,Y
- Arguments specifying the locations of
a set of points.
For
`nnwhich.ppp`

, the argument`X`

should be a point pattern (object of class`"ppp"`

). For`nnwhich.default`

, typically`X`

and - ...
- Ignored by
`nnwhich.ppp`

and`nnwhich.default`

. - k
- Integer. The algorithm finds the
`k`

th nearest neighbour. - method
- String specifying which method of calculation to use.
Values are
`"C"`

and`"interpreted"`

.

##### Details

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. If `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`

.

##### Value

- Integer vector giving, for each point, the index of its nearest
neighour (or
`k`

th nearest neighbour).

##### Warnings

A value of `NA`

is returned if there is only one point
in the point pattern.

##### See Also

##### Examples

```
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)
```

*Documentation reproduced from package spatstat, version 1.16-3, License: GPL (>= 2)*