Haversine Great Circle Distance in R (Example)


This post explains how to calculate the Haversine distance in the R programming language.

Table of contents:

Let’s dig in:


Creation of Example Data

In the first place, we need to define some data that we can use in the following examples:

my_points <- matrix(c(81.25798, 73.81277,    # Create longitude/latitude matrix
                      14.91254, 18.18145),
                    nrow = 2)
colnames(my_points) <- c("longitude", "latitude")
rownames(my_points) <- c("point_1", "point_2")
my_points                                    # Print longitude/latitude matrix


table 1 matrix haversine great circle distance r


As you can see based on Table 1, our example data is a matrix consisting of two longitudinal and latitudinal data points.


Example: Calculate Haversine Great Circle Distance Using distHaversine() Function of geosphere Package

In this example, I’ll show how to apply the distHaversine function of the geosphere package to calculate the Haversine distance of two geospatial points in R.

First, we need to install and load the geosphere package:

install.packages("geosphere")                # Install & load geosphere

Next, we can use the distHaversine function to get the distance between our two geographical points according to the Haversine formula:

my_dist <- distHaversine(my_points)          # Calculate Haversine distance
my_dist                                      # Print Haversine distance
# [1] 873680.6

The Haversine distance between our points is 873680.6.


Video, Further Resources & Summary

Have a look at the following video instruction of my YouTube channel. I’m explaining the R code of this article in the video.


The YouTube video will be added soon.


In addition, you might have a look at some of the other tutorials on my website. Please find a selection of tutorials about distance measures here:


Summary: You have learned in this tutorial how to compute the Haversine great circle distance in R programming. Don’t hesitate to let me know in the comments below, if you have further questions. In addition, don’t forget to subscribe to my email newsletter in order to get updates on the newest tutorials.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.