Skip to content
Prev 139324 / 398506 Next

Distances between two datasets of x and y co-ordinates

Andrew McFadden said the following on 3/12/2008 1:47 PM:
If you're trying to find only the closest point in data1 to data2, then 
use knn (or knn1) in the 'class' package:

library(class)
nn <- knn1(data2, data1, 1:nrow(data2))

which gives you the rows in data1 closest to each row in data2. Then 
compute the distance:

rowSums((data2[nn, ] - data1)^2)^0.5

HTH,

--sundar