0
votes

I'm trying to calculate the distance between two coordinates (40.777250, -73.872610) and (40.6895, -74.1745).

Using distm in R gives the following result:

> distm (c(40.777250, -73.872610), c(40.6895, -74.1745), fun = distHaversine)

33713.61

When I use Excel to calculate the distance with the following function

6378134 * ACOS(COS(RADIANS(90-40.777250)) *COS(RADIANS(90-40.6895)) +SIN(RADIANS(90-40.777250)) *SIN(RADIANS(90-40.6895)) *COS(RADIANS(-73.872610-(-74.1745))))

the answer is 27274.49199.

I'm wondering why these two methods give a different answer and whether I'm doing something wrong. I tried an online coordinate distance calculator and it gives the same answer as my excel function.

1

1 Answers

2
votes

You have to change the order of longitude and latitude , because (see vignette):

Geographic locations must be specified in longitude and latitude (and in that order!)

result <- distm (c(-73.872610,40.777250), c(-74.1745, 40.6895), fun = distHaversine)
#         [,1]
# [1,] 27274.5

or:

distHaversine(c(-73.872610,40.777250), c(-74.1745, 40.6895))
# [1] 27274.5