Function to calculate geospatial distance between two points (lat,long) using R

RPackageGeospatialDistanceLatitude Longitude

R Problem Overview


I have geocoded points in long, lat format, and I want to calculate the distance between them using R. This seems pretty straight forward, yet I can't find a function that will do it easily. I've been attempting to do it with the gdistance package, but it seems very convoluted and oriented to graphing, I just need a number. Something like distanceBetween(pointA,pointB) that returns a number.

R Solutions


Solution 1 - R

Loading the geosphere package you can use a number of different functions

library(geosphere)
distm(c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

Also:

distHaversine()
distMeeus()
distRhumb()
distVincentyEllipsoid()
distVincentySphere()

...

Solution 2 - R

Agree with @PereG on answer above, but think that the order of latitude and longitude is the other way around: lon, lat. This will affect your results for distance matrix. So correct is:

library(geosphere)
distm (c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

Source: ftp://cran.r-project.org/pub/R/web/packages/geosphere/geosphere.pdf

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveO7View Question on Stackoverflow
Solution 1 - RPereGView Answer on Stackoverflow
Solution 2 - RAgustín IndacoView Answer on Stackoverflow