0

I have calculated the nearest location between two points based on the given latitude and longitude using geosphere. The issue I have is the distance is not in kilometers or meters. How can I get the calculated distance in KM or M?

my original dataset

t1 <-
structure(list(UML_ID = c("PO1000008590", "Submitted to WRI for review", 
"PO1000003711", "PO1000004269", "PO1000008154"), Latitude = c(17.00352, 
7.2, 1.545386, 2.051269, 2.11272), Longitude = c(81.46973, -2.1173, 
104.209347, 100.252339, 100.27311), Country = c("India", "Ghana", 
"Malaysia", "Indonesia", "Indonesia")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame"))


library(geosphere)

d.t1 <- distm(t1[,c('Longitude','Latitude')], fun = distGeo)
min.d.t1 <- apply(d.t1, 1, function(x) order(x, decreasing = F)[2]) 
new.t1 <- cbind(t1, t1[min.d.t1,], apply(d.t1, 1, function(x) sort(x,decreasing = F)[2]))
colnames(new.t1) <- c(colnames(t1), paste("n.", colnames(t1)), "Distance")

However the distance I get are not in KM nor m.

F H
  • 43
  • 5
  • 2
    At the moment, the code doesn't run, for example `d.Irf` is not defined, you'll need to fix that before the question can be answered. The function `distGeo` returns the distance in m, and that seems to be what's shown in `d.t1` (unless you think that's wrong?), so the problem is with extracting the relevant numbers from the distance matrix – Miff Jul 14 '21 at 11:06
  • yes suppose to be d.t1 – F H Jul 14 '21 at 13:45
  • 1
    Great. Why don't you think that the distances calculated are the correct number of metres? – Miff Jul 14 '21 at 14:18
  • Thank you for pointing out the error in my original code. Once I fix it.. the number correct in meters. – F H Jul 17 '21 at 15:45

0 Answers0