I have 2 dataframes that are simply matrices of 2 dimensions (lat/long). Both dataframes would look like the input below:
latitude longitude
27.78833 -82.28197
27.79667 -82.29294
Let's call them "dfref" and "dfnew". I would like to find the nearest point in dfnew
for each point in dfref
and the distance between the 2 points in meters.
The output would look like this:
dr.latitude dr.longitude dn.latitude dn.longitude dist
27.78833 -82.28197 27.54345 -82.33233 162.34
27.79667 -82.29294 27.56543 -82.12323 232.23
I have tried using the knn function in the class package and the Searchtrees package but my script only found the nearest points in the dfref matrix and I am not sure how to add the measurement.
knn1(train=cbind(dfref), test=cbind(dfnew), cl=seq_len(nrow(dfnew)))
Is there a function that does both efficiently and how can I get this into one script?