I currently have two data frames. Each data frame consists of an index, a longitude and a latitude. The index doesn't have much of a real value. It's just there to be able to identify the places.
Example input:
df1 <- data.frame(
index1 = c('1','2','3','4','5','6'),
lon1 = c(-95.800781,-95.855873,-95.923451,-95.785214,-94.658239,-95.471652),
lat1 = c(42.29118,43.36395,43.50854,43.37096,48.07726,43.214579))
df2 <- data.frame(
index2 = c('A','B','C','D','E'),
lon2 = c(-96.800175,-96.537616,-96.00984,-95.92965,-95.471652),
lat2 = c(43.34639,47.85308,42.79573,43.42444,44.27002))
Aim:
I'm looking for a way to calculate the (euclidean preferably) distance between places in the first table and places in the second table. Ideally, I'd want to be able to have the shortest distance between a place in df1 and df2, for every observation in df1. So, it's possible that not every observation in df2 will be used to calculate a shortest distance.
I've attempted to attack this problem by using the following below. The reason Jaap's solution doesn't work for me is because I don't have locality. Perhaps I'm understanding this wrong. Any update would be appreciated.
Geographic distance between 2 lists of lat/lon coordinates
AND
earth.dist <- function (Lon1, Lat1, Lon2, Lat2)
{
rad <- pi/180
a1 <- Lat1 * rad
a2 <- Lon1 * rad
b1 <- Lat2 * rad
b2 <- Lon2 * rad
dlon <- b2 - a2
dlat <- b1 - a1
a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2
c <- 2 * atan2(sqrt(a), sqrt(1 - a))
R <- 6378.145
d <- R * c
return(d)
}