Function to calculate geospatial distance between two points (lat,long) using 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


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()

...