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!