[英]Calculating distance between two classes of latlong points
I have a series of latlong coordinates in R which are divided among two individuals: 我在R中有一系列的latlong坐标,分为两个人:
name lat long
A -28.63784 28.69085
A -28.65366 28.70843
A -28.80918 28.94223
B -26.71335 22.80713
B -26.75022 20.58426
B -34.37791 20.51215
How do I calculate the distance between the coordinates of one individual to the other but not to itself? 如何计算一个人与另一个人之间的坐标,而不是与自己之间的距离? I've looked at similar questions on here but I can't see anything that will do the grouping for me.
我在这里查看过类似的问题,但看不到任何可以为我进行分组的内容。
Thanks 谢谢
First create two lon/lat data frames, one for individual A
and one for B
: 首先创建两个lon / lat数据帧,一个用于单个
A
,另一个用于B
:
locationsA <- subset(d, name == "A", select = c("long", "lat"))
locationsB <- subset(d, name == "B", select = c("long", "lat"))
The rdist.earth function in the fields package can then compute the matrix of distances of all pairings: 然后,fields包中的rdist.earth函数可以计算所有配对的距离矩阵:
library(fields)
dists <- rdist.earth(locationsA, locationsB)
For the six rows you showed, for example, these distances are (in miles): 例如,对于您显示的六行,这些距离是(以英里为单位):
[,1] [,2] [,3]
[1,] 384.1700 513.2758 624.3275
[2,] 385.5346 514.5599 624.4023
[3,] 402.4771 530.8293 628.1450
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.