i get this error "Error in .pointsToMatrix(p1) : latitude > 90". Can
anyone explain why and how to solve?
The error tells you that you got latitude values greater than 90, which is out of scope:
library(geosphere)
distHaversine(c(4,52), c(13,52))
# [1] 616422
distHaversine(c(4,52), c(1,91))
# Error in .pointsToMatrix(p2) : latitude > 90
You can solve this issue by only feeding distHaversine
with coordinates inside the accepted ranges.
I am trying to use the distHavrsine function in R, inside a loop to
calculate the distance between some latitude and longitude coordinates
for a several hundred rows. (...) if the distance is less than 50
meters i want it to record those rows
Have a look at the distm
function, which calculates a distance matrix for your few hundred rows easily (i.e. without loops). It uses distHaversine
by default. For example, to get the data frame rows that are closer then 650000 meters:
df <- read.table(sep=",", col.names=c("lon", "lat"), text="
4,52
13,52
116,39")
(d <- distm(df))
# [,1] [,2] [,3]
# [1,] 0 616422 7963562
# [2,] 616422 0 7475370
# [3,] 7963562 7475370 0
d[upper.tri(d, T)] <- NA
( idx <- which(d < 650000, arr.ind = T) )
# row col
# [1,] 2 1
cbind(df[idx[, 1], ], df[idx[, 2], ])
# lon lat lon lat
# 2 13 52 4 52