Skip to content

Instantly share code, notes, and snippets.

@rukku
Created February 20, 2012 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rukku/1867642 to your computer and use it in GitHub Desktop.
Save rukku/1867642 to your computer and use it in GitHub Desktop.
Second nearest neighbor with crossdist using apply()
#get pairwise distances from rp and ponderosa datasets
xpairs <- crossdist(rp, ponderosa)
#A function to get the second nearest neighbor
nncross2 <- function (x) sort(x)[2]
#Initialize the vector container for x.2
x.2 <- numeric()
#Applies the nncross2 function to the xpairs matrix.
x.2 <- apply(xpairs, 1, nncross2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment