I have 54 points in one data set A I have 300+ data points in a dataset B.

I wish to find the distance for each point in dataset-set B, to points in dataset A.

Nearest Neighbor packages appear to only find neighbors within a given data-set, not a second data set.

rdist was close, but I didn't want a matrix, just nearest.--"Given two sets of locations computes the full Euclidean distance matrix among all pairings or a sparse version for points within a fixed threshhold distance."

This looked good, saying "Read two geocoded point sets from Comma Separated Value (CSV) files into R data objects. Assign to each member of first point set the geographically closest point from second set" but is so dated (2010), so something more recent must exist...

Same issue with this

Other packages find distances between points and lines, points and polygons, etc. viz: http://cran.r-project.org/web/packages/geosphere/geosphere.pdf

The omission of P2P suggests I am missing something simple...

  • What nearest neighbour packages? kdtree in nabor is likely to be the most efficient, but if you need ellipsoid distances you can roll your own with spDistsN1 in sp.
    – mdsumner
    Dec 20, 2014 at 1:00
  • Have you considered using GRASS GIS for the distance analysis? I'm sure googling would come up with the correct "v." command, then invoke R fromwithin GRASS? May 11, 2016 at 6:45

2 Answers 2



The FNN package has a function get.knnx which can compute the N-nearest neighbours in point patterns. For what you want, this should work:

nn = get.knnx(A,B,k=1)

Which should just return the nearest neighbors between the two datasets. You can also specify what nearest neighbor algorithm it will use, be it kd_tree, cover_tree, CR, or brute force.

spatstat has a crossdist function.


Computes the distances between pairs of ‘things’ taken from two different datasets.

It takes two point patterns A and B as inputs, and returns the matrix whose [i,j] entry is the distance from A[i] to B[j]. To get the nearest neighbors between two datasets using crossdist:

xdistances <- crossdist(A, B)  #Get all cross distances    

nn = numeric() 
for (i in 1:nrow(A)) {   
  xdistance <- sort(xdistances[i,], partial=1)[1]   
  nn <- append(nn, xdistance)

I hope that helps.

  • This does solve the problem I've articulated. But I'm also hoping to use the function on a much larger table, where set B contains 5K pts, or 50K points, or 500K points. It just seems like there should exist an R package that doesn't waste computation so, that would just find the distance to the nearest point, rather than all points.
    – Mox
    Dec 22, 2014 at 5:49
  • Updated my answer. I hope it works.
    – R.K.
    Dec 22, 2014 at 6:14
  • Thank you. I am much obliged. Your solution does seem to be the general one--I see it and similar solutions cropping up over and over, like so: stackoverflow.com/questions/22121742/…
    – Mox
    Dec 22, 2014 at 6:17
  • 1
    If you found it useful, do upvote it. It sends the message that it did answer your question to some extent and it'll give me the peace of mind that I didn't just waste a fraction of my life trying to solve problems for random strangers on the internet. ;-)
    – R.K.
    Dec 22, 2014 at 6:31
  • Happily. My apologies for not doing so sooner.
    – Mox
    Dec 22, 2014 at 6:35

RANN looked like the package I was looking for, but was not... RANN gives the nearest neighbor for CELLS, not an XY point layer.

FNN was the package I was looking for.

Alternates methods exist, but share commonalities: All methods make use of a) A distance function b) loop or apply c) A 'min' of the distance function




  • Could you give some examples of how you made it work?
    – Simbamangu
    Dec 21, 2014 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.