The entries on the diagonal of a spatial distance matrix should be zero, bc they represent the distance between each location and itself. But the rdist.earth()
function from the fields
R package
sometimes give me non-zeros on the diagonal:
> # Set number of decimals of output display
> options(digits=8)
> # Some longitude, latitude data
> LLdat
lon lat
1 -105.85878 43.65797
2 -105.81812 43.57009
3 -105.80796 43.57748
>
> # Create distance matrix
> library(fields)
> distmat <- rdist.earth(LLdat,LLdat)
> distmat
1 2 3
1 0.0000000 6.410948951394 6.12184338
2 6.4109490 0.000059058368 0.72150586
3 6.1218434 0.721505863563 0.00000000
In the above distance matrix, the second entry on the diagonal is 0.000059058368
, in miles (the default unit), whereas the other two are 0.0000000
. First, why do the entries of the second column show more digits than the other two? And why is the entry on the second diagonal not zero to 8 decimals like the others? The discrepancy doesn't seem small enough to attribute to floating point rounding error.
Now compare the output of rdist.earth()
to that of a different package, geosphere
, and function distGeo()
, which computes the distance between two points (not a full distance matrix). Here, we compute the distance between each point and itself. The output vector units are in meters:
> library(geosphere)
> distmat2 <- distGeo(LLdat,LLdat)
> distmat2
[1] 0 0 0
So with distGeo()
, all three distances measures are in agreement and appropriately zero.
Is there something I'm missing? Or does this indicate a problem with rdist.earth()
?