| nn | R Documentation | 
nn determines the nearest neighbors for a set of 
observations based on a distance matrix.
nn(d, k, method = "c", self = FALSE)
d | 
 A square distance matrix for the coordinates of interest.  | 
k | 
 The number of neighbors to return (if 
  | 
method | 
 The method of determining the neighbors. 
The default is   | 
self | 
 A logical indicating whether an observation 
is a neighbor with itself.  The default is 
  | 
This function determine nearest neighbors in two ways: 1. number of neighbors or 2. distance.
If method = "c", then k specifies the total
number of neighbors to return for each observation.  
If method = "d", then k specifies the maximum
distance for which an observation is considered a 
neighbor.  
The function returns the neighbors for each observation.
Returns a list with the nearest neighbors of each observation. For each element of the list, the indices order neighbors from nearest to farthest.
Joshua French
data(grave)
# make distance matrix
d = as.matrix(dist(cbind(grave$x, grave$y)))
# 3 nearest neighbors
nnc = nn(d, k = 3, method = "c")
# nearest neighbors within k units of each observation
nnd = nn(d, k = 200, method = "d")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.