0

For starters, here is the table structure I'm using:

df <- structure(list(customer_id = c(353808874L, 69516747L, 357032052L, 
307735090L, 307767260L), id = c("8474", "8107", 
"1617436", "7698", "1617491"), lon1 = c(-115.032623, -115.155029, 
-115.270386, -115.19426, -115.177589), lat1 = c(36.0437202, 36.1366234, 
36.1678734, 36.2635803, 36.2218285), lon2 = c(-115.037022076035, 
-115.150112230012, -115.27341017806, -115.193072645577, -115.174902476442
), lat2 = c(36.0410001245783, 36.141137860928, 36.1700923382169, 
36.2682687632778, 36.2240270452917)), row.names = c(NA, 5L), class = "data.frame")

I have attempted using the info from several different Stack Overflow questions, but none have reached the desired outcome.

One of them was this:

earthDist <- 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)
}

earthDist(lon[1], lat[1], lon, lat)

But I could not get it to produce the output I'm looking for. I'm certainly not attached to it, so if anyone has something more effective, I'm all ears!

EDIT: My intended outcome is rather simple. Just three columns, distance_between representing the distance between lon/lat1 and lon/lat2:

+-------------+----+------------------+
| customer_id | id | distance_between |
+-------------+----+------------------+
  • 1
    Does this answer your question? [How to calculate distance and time between two locations](https://stackoverflow.com/questions/59772556/how-to-calculate-distance-and-time-between-two-locations) – jay.sf Jan 22 '20 at 20:02
  • Try `library(geosphere);distm(df[c('lon1', 'lat1')], df[c('lon2', 'lat2')])` – akrun Jan 22 '20 at 20:03
  • @akrun I keep getting the following error: `Error: vector memory exhausted (limit reached?)` I guess I should have noted that the dataframe above was just a small sample of a much larger one. Any ideas? –  Jan 22 '20 at 20:28
  • @jay.sf - No it does not. I keep trying to install the `VTrack` package but my environment doesn't seem to let me –  Jan 22 '20 at 20:40
  • @Dave2e I will make an edit –  Jan 22 '20 at 20:56
  • https://stackoverflow.com/q/36817423/5977215. Also, `geosphere` is quite slow and inefficient. I recommend [`library(geodist)`](https://github.com/hypertidy/geodist). I've also done a few benchmarks [here](https://gist.github.com/SymbolixAU/0f65a431a53da6bdaf5d60b8ff30eba9) – SymbolixAU Jan 22 '20 at 22:33

1 Answers1

1

This is a easily solved with the distGeo function (similar to your functions above) from geosphere package:

library(geosphere)
#calculate distances in meters
df$distance<-distGeo(df[,c("lon1", "lat1")], df[,c("lon2", "lat2")])

#remove columns
df[, -c(3:6)]

customer_id      id distance
1   353808874    8474 498.2442
2    69516747    8107 668.4088
3   357032052 1617436 366.9541
4   307735090    7698 531.0785
5   307767260 1617491 343.3051
Dave2e
  • 22,192
  • 18
  • 42
  • 50