Calculating geographic distance between latitude and longitude

Hi all,

I have been tasked with calculating the distance of two locations for several individuals. As seen in the image, I have a subset of 5 "IDs" that contain a starting "lat" and "long" and a final "lat" and "long". Row 1 & 2 belong to one individual, 3 & 4 the next, so on and so forth.

I have seen people say that using the geosphere package works, however I have a large dataset and have not found a way to apply that package to my data. I began calculating some of them by typing them out, however it would be great to learn of a way to extract specific rows and columns automatically. Perhaps I am missing a detail of this method, any suggestions/help would be much appreciated!

1 Like

I have not used the geoshpere package, but looking at the documentation of the distGeo() function, something like the following should work. The code makes separate matrices of the starting and ending positions by selecting odd and even rows. The last step is marked as a comment because I don't have the package to test it.

#Invent some data
DF <- data.frame(ID = c(1,1,2,2,3,3),
                 Date = c("2013-06-01", "2013-07-01",
                          "2013-06-07", "2013-07-13",
                          "2013-06-02", "2013-07-21"),
                 Lat = c(41,43,37,41,45,44),
                 Long = c(-71,-67,-89,-92,-73,-80))
#Make a matrix of the starting locations
Start <-  DF[seq(1,nrow(DF) - 1, by = 2), c("Long", "Lat")]
Start <- as.matrix(Start)
Start
#>   Long Lat
#> 1  -71  41
#> 3  -89  37
#> 5  -73  45

#Make a matrix of the ending locations
End <-  DF[seq(2,nrow(DF), by = 2), c("Long", "Lat")]
End <- as.matrix(End)
End
#>   Long Lat
#> 2  -67  43
#> 4  -92  41
#> 6  -80  44

#library(geosphere)
#Distances <- distGeo(p1 = Start, p2 = End)

Created on 2022-08-27 by the reprex package (v2.0.1)

1 Like

Hi!

I formatted my data similarly to your example and it worked great! Thank you so much :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.