0

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)
} 
Community
  • 1
  • 1
usr
  • 782
  • 1
  • 7
  • 25
  • 1
    Instead of writing your own, you might try using the function `distGeo` in the `geosphere` package. – G5W Mar 31 '17 at 14:19
  • Is your question about how to calculate the distance, or about how to structure the process? Seems you've already found some good solutions (and @G5W suggested another) for the former. Can you be more precise about the problem you're having? – Andrew Cheesman Mar 31 '17 at 14:54
  • You should better describe what you mean with *"none of this worked out well"* as the approach in my answer in the linked question is exactly what you need based on your description above. – Jaap Mar 31 '17 at 14:54
  • @Jaap FYI I've changed the description. Does that make sense for you? – usr Apr 03 '17 at 09:19
  • The lack of `locality` isn't a drawback. Instead, you have `index1` and `index2` with which you should be able to solve it. Especially, see the last part of [my answer](http://stackoverflow.com/a/31668415/2204410) which adresses the calculation of the shortest distance. – Jaap Apr 03 '17 at 09:34
  • @G5W Your suggestion seemed to work at first, but after further investigation it doesn't seem like it is calculating the distance using the shortest location. – usr Apr 03 '17 at 14:42
  • @usr Interesting. Can you provide a pair of points for which it seems to be producing an answer that is not the shortest distance? – G5W Apr 03 '17 at 15:00
  • @G5W The reason I know is because for several it is calculating the distance between a point and another point that is located on the other side of the planet (Australia). There is no way that can be the closest point. – usr Apr 03 '17 at 15:28
  • @Jaap What unit is the near_dist expressed in? – usr Apr 03 '17 at 15:40
  • The distance is expressed in meters. – Jaap Apr 04 '17 at 05:54

0 Answers0