Reputation: 1441
I have a series of latlong coordinates in R which are divided among two individuals:
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
Upvotes: 0
Views: 160
Reputation: 78590
First create two lon/lat data frames, one for individual A
and one for 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:
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
Upvotes: 4