Kajan Sittampalam - 9 months ago 64

R Question

I am trying to calculate the distance between two points on earth using R.

`library(geosphere)`

library(oce)

geodDist(StoreLocations$latitude[c(26)], StoreLocations$longitude[c(26)],

CustomerLocations$latitude[c(5)], CustomerLocations$longitude[c(5)])

Putting in the Lat/Long in google maps (measure distance feature). I get total distance: 1.88 km (1.17 mi) but this is not what I am getting using the code above. I am struggling to find anyone who can assist me with this.

`Store 51.68108210 -0.09732268`

Customer 51.66665932 -0.08300349

Answer

From reading the `geosphere`

documentation it looks like you've reversed the argument order. `geoDist`

(or `distGeo`

in the most recent version of the package, released June 15 2016) expects longitude first, then latitude. You seem to have latitude first.

```
df <- data.frame(long = c(-0.09732268, -0.08300349), lat = c(51.68108210, 51.66665932))
long lat
1 -0.09732268 51.68108
2 -0.08300349 51.66666
distGeo(df[1, ], df[2, ])
1885.795 (1.88 km)
```

You should also check to ensure that you are using the most current version of the package.