rmatrixpairwisepairwise-distance

Generating a pairwise 'distance' matrix


I am trying to generate what I can best describe as a pairwise distance matrix from a data frame containing the distance between two neighboring points. These are not Euclidean distances, they are essentially the distance between points on a shoreline, so the distance is not a straight line. I was able to generate a distance matrix in the package riverdist using geospatial data, but that only did the complete distance between the two points, I am now trying to do a subset of this distance between the points.

I have been looking for a way to do this for a little while and keep coming up empty handed. Any help would be much appreciated.

Here is an exampe:

I have this data:

mat <- matrix(
 c(
     3, #distance between 1 and 2
    10, #distance between 2 and 3
     7, #distance between 3 and 4
     9  #distance between 4 and 5
),
nrow=4, ncol=1, dimnames =     list(c("site1","site2","site3","site4"),c("dist")))

> mat
  dist
site1    3
site2   10
site3    7
site4    9

And I would like to produce the following 'distance' matrix:

     site1 site2 site3 site4 site5
site1    0
site2    3     0     
site3   13    10     0   
site4   20    17     7     0
site5   29    26    16     9     0

The original data might be better organized as follows for this task:

     SiteA SiteB Dist
   1 site1 site2    3
   2 site2 site3   10
   3 site3 site4    7
   4 site4 site5    9

Any advice out there?


Solution

  • This is a cumulative distance, so take a cumulative sum and then do the distance calculation:

    mat <- c(3,10,7,9)
    dist(cumsum(c(0,mat)))
    #   1  2  3  4
    #2  3         
    #3 13 10      
    #4 20 17  7   
    #5 29 26 16  9