nnwhich {spatstat}R Documentation

Nearest neighbour

Description

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

Usage

  nnwhich(X, ..., method="C")
  ## S3 method for class 'ppp':
  nnwhich(X, ..., method="C")
  ## Default S3 method:
  nnwhich(X, Y=NULL, ..., 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 Y would be numeric vectors of equal length. Alternatively Y may be omitted and X may be a list with two components x and y, or a matrix with two columns.
... Ignored by nnwhich.ppp and nnwhich.default.
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.

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 is only one point (if x has length 1), then the nearest neighbour is undefined, and a value of NA is returned. 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 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.

Warnings

A value of NA is returned if there is only one point in the point pattern.

Author(s)

Pavel Grabarnik pavel.grabar@issp.serpukhov.su and Adrian Baddeley adrian@maths.uwa.edu.au http://www.maths.uwa.edu.au/~adrian/

See Also

nndist, nncross

Examples

   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)

[Package spatstat version 1.11-3 Index]