Learn R Programming

spatstat (version 1.11-4)

nncross: Nearest Neighbour in Another Point Pattern

Description

Given two point patterns X and Y, finds the nearest neighbour in Y of each point of X.

Usage

nncross(X, Y, iX=NULL, iY=NULL)

Arguments

X, Y
Two point patterns (objects of class "ppp").
iX, iY
Optional identifiers, used to test whether a point in X is identical to a point in Y. See Details

Value

  • A data frame with two columns:
  • distNearest neighbour distance
  • whichNearest neighbour index in Y

Details

Given two point patterns X and Y this function finds, for each point of X, the nearest point of Y. The distance between these points is also computed.

The return value is a data frame, with rows corresponding to the points of X. The first column gives the nearest neighbour distances (i.e. the ith entry is the distance from the ith point of X to the nearest point of Y). The second column gives the indices of the nearest neighbours (i.e. the ith entry is the index of the nearest point in Y.)

Note that this function is not symmetric in X and Y. To find the nearest neighbour in X of each point in Y, just use nncross(Y,X).

The arguments iX and iY are used when the two point patterns X and Y have some points in common. In this situation nncross(X, Y) would return some zero distances. To avoid this, attach a unique integer identifier to each point, such that two points are identical if their identifying numbers are equal. Let iX be the vector of identifier values for the points in X, and iY the vector of identifiers for points in Y. Then the code will only compare two points if they have different values of the identifier. See the Examples.

Examples

Run this code
# two different point patterns
  X <- runifpoint(15)
  Y <- runifpoint(20)
  N <- nncross(X,Y)$which
  # note that length(N) = 15
  plot(superimpose(X=X,Y=Y), main="nncross", cols=c("red","blue"))
  arrows(X$x, X$y, Y[N]$x, Y[N]$y, length=0.15)

  # two patterns with some points in common
  Z <- runifpoint(50)
  X <- Z[1:30]
  Y <- Z[20:50]
  iX <- 1:30
  iY <- 20:50
  N <- nncross(X,Y, iX, iY)$which
  plot(superimpose(X=X, Y=Y), main="nncross", cols=c("red","blue"))
  arrows(X$x, X$y, Y[N]$x, Y[N]$y, length=0.15)

Run the code above in your browser using DataLab