# nnwhich.pp3

##### Nearest neighbours in three dimensions

Finds the nearest neighbour of each point in a three-dimensional point pattern.

##### Usage

```
# S3 method for pp3
nnwhich(X, …, k=1)
```

##### Arguments

- X
Three-dimensional point pattern (object of class

`"pp3"`

).- …
Ignored.

- k
Integer, or integer vector. The algorithm will compute the distance to the

`k`

th nearest neighbour.

##### Details

For each point in the given three-dimensional
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
each point's `k`

th nearest neighbour.

The function `nnwhich`

is generic. This is the method
for the class `"pp3"`

.

If there are no points in the pattern,
a numeric vector of length zero is returned.
If there is only one point,
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

Numeric vector or matrix giving, for each point,
the index of its nearest neighbour (or `k`

th nearest neighbour).

If `k = 1`

(the default), the return value is a
numeric vector `v`

giving the indices of the nearest neighbours
(the nearest neighbout of the `i`

th point is
the `j`

th point where `j = v[i]`

).

If `k`

is a single integer, then the return value is a
numeric vector giving the indices of the
`k`

th nearest neighbours.

If `k`

is a vector, then the return value is a
matrix `m`

such that `m[i,j]`

is the
index of the `k[j]`

th nearest neighbour for the
`i`

th data point.

##### Warnings

A value of `NA`

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

##### See Also

##### Examples

```
# NOT RUN {
X <- runifpoint3(30)
m <- nnwhich(X)
m2 <- nnwhich(X, k=2)
# }
```

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