How to calculate distance between coordinates (for multiple rows)?

Hi

I'm trying to calculate the distance between lon/lat coordinates of different samples. I can calculate individual distances, but I want to do it for my full dataset. Any idea how to?

Example:
library(geosphere)
distm(c(103.852, 22.4191), c(16.267, 47.867), fun=distHaversine)

This calculates the distance between two points. But I want to do it for the full dataset. This following function:

library(geosphere)
korfinal$CurrentLat <- as.numeric(korfinal$CurrentLat)
korfinal$CurrentLon <- as.numeric(korfinal$CurrentLon)
korfinal$MatchLat <- as.numeric(korfinal$MatchLat)
korfinal$MatchLon <- as.numeric(korfinal$MatchLon)

distm(c(korfinal$CurrentLon[1], korfinal$CurrentLat[1]), c(korfinal$MatchLon[1], korfinal$MatchLat[1]), fun=distHaversine)

calculates the value of the first row and it works. But is there any way with the "loop" function to have it repeat this for every row? I've tried a bit but failed.

Here's a small section from my dataset.

data.frame(
stringsAsFactors = FALSE,
Matchproid = c("ABOLB141-15","ABOLD049-16",
"ABOLD057-16","ABOLD581-17","ABOLD663-17","AGIRI015-17"),
Sampleid = c("AVM_78","AVM_81","AVM_78",
"AVM_237","AVM_82","AVM_122"),
Currentid = c("Pontia edusa",
"Pieris brassicae","Pontia edusa","Neptis sappho","Pieris rapae",
"Danaus chrysippus"),
Matchp = c("100", "99.67", "100", "97.06", "99.84", "100"),
Matchid = c("Pontia edusa",
"Pieris brassicae","Pontia edusa","Neptis sappho","Pieris rapae",
"Danaus chrysippus"),
Family = c("Pieridae","Pieridae",
"Pieridae","Nymphalidae","Pieridae","Nymphalidae"),
CurrentLat = c("22.4191","22.4191","22.4191",
"22.4191","22.4191","22.4191"),
CurrentLon = c("103.852","103.852","103.852",
"103.852","103.852","103.852"),
CurrentElev = c(1300, 1300, 1300, 1300, 1300, 1300),
MatchLat = c("47.867","48.033","47.883",
"47.9719","47.825","12.9716"),
MatchLon = c("16.267","16.25","16.283",
"16.6306","16.0436","77.5946"),
MatchElev = c("240", "300", "270", "250", "620", "920")
)

Thanks!

Hello,

R has a built in function to easily calculate the distance matrix from a set of coordinates like this:

set.seed(1) #For reproducibility only

#Data with x and y coords
points = data.frame(x = runif(5), y = runif(5))
points
#>           x          y
#> 1 0.2655087 0.89838968
#> 2 0.3721239 0.94467527
#> 3 0.5728534 0.66079779
#> 4 0.9082078 0.62911404
#> 5 0.2016819 0.06178627

#Distance matrix
dist(points, method = "euclidean", upper = T, diag = T)
#>           1         2         3         4         5
#> 1 0.0000000 0.1162289 0.3884722 0.6968296 0.8390346
#> 2 0.1162289 0.0000000 0.3476762 0.6220650 0.8991904
#> 3 0.3884722 0.3476762 0.0000000 0.3368478 0.7046865
#> 4 0.6968296 0.6220650 0.3368478 0.0000000 0.9061124
#> 5 0.8390346 0.8991904 0.7046865 0.9061124 0.0000000

Created on 2022-03-06 by the reprex package (v2.0.1)

Hope this helps,
PJ

1 Like

Works, thank you very much!

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.