If you are dealing with data that has a spatial component you may find yourself wanting to calculate the geographical distance (as the crow flies) between two points.
The first task is to source the latitude and longitude values for both points. These co-ordinates should be in the form of decimal degrees (DD). For example, the rough location of New Zealand’s Little Barrier Island in DD is (-36.200965, 175.080992).
If you don’t have co-ordinates on hand, you can easily get them from google maps by following these instructions.
If your co-ordinates take another form, perhaps degrees, minutes, and seconds (DMS) or degrees and decimal minutes (DMM), several converters exist online.
When you have your co-ordinates, you can calculate the distance between them by calling the distance functions contained in the R package geosphere. There are a handful of functions to choose from, each reflecting a different approach to distance calculation. For example, distHaversine() assumes the earth is spherical while distVincentyEllipsoid() assumes the earth is an ellipsoid (a great discussion on this can be found here). geosphere is a really interesting package, I would recommend having a read through the documentation as it is loaded with interesting functions.
By way of an example, lets say you want to calculate the distance in metres between Mount Ruapehu and Mount Ngauruhoe, two volcanoes found in New Zealand’s Central Plateau.
# loading package library(geosphere) # mount ruapehu ruapehu = c(175.568450,-39.281726) # mount ngauruhoe ngauruhoe = c(175.632122,-39.156874) # distance between ruapehu and ngauruhoe in metres distVincentyEllipsoid(p1=ruapehu, p2=ngauruhoe)
Just divide the result by 1000 if you want kilometres.
As well as providing us with co-ordinates, google maps allows us to validate our results by measuring the distance between two points . Instructions for how to do this can be found here.