Finding nearest neighbour (log, lat), then the nex

2019-07-28 07:54发布

问题:

I have two datasets (csv format) of different lengths, both containing addresses as well as coordinates (log and lat). The both have this shape (example Dataset A):

ID  Address lat long
1   Lausitzer Strasse, 20/22, 2991, Lauta, Germany  51.46228    14.09522
2   Parkstrasse, 6, 2991, Lauta, Germany    51.4631141  14.1109184
3   Parkstrasse, 6, 2991, Lauta , Germany   51.4631141  14.1109184
4   Arndtstrasse, 27, 2991, Lauta, Germany  51.44664    14.10287
5   Goethestrasse, 13, 4746, Hartha, Germany    51.0965679  12.9725834
6   Bahnhofstrasse, 3, 3a, 3b, 4886, Beilrode, Germany  51.57183    13.06729
7   Haefenmarkt, 11, 98673, Eisfeld, Germany    50.42697    10.90871

As mentioned my other Dataset (B) looks like the one above (A). I am trying to find a way to find the closest address for each point in Dataset A.

Dataset A is fairly small (22 addresses) but Dataset B is quite large (8500 addresses).

I am new to R and some advice would greatly be appreciated.

Thanks.

EDIT:

I made the code work like this (below), however, is there a way to search for not only the nearest but also the second nearest etc. neighbour?

list1 <-read.csv("geocoded.csv",sep=",",header=TRUE) 
list2 <- read.csv("geocoded2.csv", sep=",", header=TRUE)

library(geosphere)
library(data.table)
library(dplyr)

mat <- distm(list1[,c('long','lat')], list2[,c('long','lat')],     
fun=distVincentyEllipsoid)

# assign the name to the point in list1 based on shortest distance in the 
matrix
list1$locality <- list2$locality[apply(mat, 1, which.min)]

#list2a <- list2 %>% group_by(locality) %>% summarise_each(funs(mean)) %>%     
ungroup()
#mat2 <- distm(list1[,c('long','lat')], list2a[,c('long','lat')], 
fun=distVincentyEllipsoid)
#list1$locality2 <- list2a$locality[apply(mat2, 1, which.min)]

list1

#Plot distance

#list1$near_dist <- apply(mat2, 1, min)

list1$near_dist <- apply(mat, 1, min)

list1
write.table(list1, "C:/v", sep=",", row.names=FALSE)

Thank you.