[英]Using distm function in R to calculate distance between two coordinates gives a different answer than when calculating the same thing in excel
I'm trying to calculate the distance between two coordinates (40.777250, -73.872610) and (40.6895, -74.1745)
. 我正在尝试计算两个坐标(40.777250, -73.872610) and (40.6895, -74.1745)
之间的距离。
Using distm in R gives the following result: 在R中使用distm可获得以下结果:
> 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 当我使用Excel通过以下函数计算距离时
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
. 答案是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. 我尝试了一个在线坐标距离计算器,它给出的答案与我的excel函数相同。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.