2
votes

I want to create a distance matrix based on two columns which represent Point IDs and a column representing my "distance". The distances are actually normalized maximum slope values, so basically i want a distance matrix on the maximum slope between two points. I have one row for every connection which looks somehow like this:

Origin = c(10001,10001,10002,10002,20001,20001)
Destin = c(10002,20001,10001,20001,10001,10002) 

and a third vector with the maximum slope values:

maxSlope = c(0.47, 0.12, 0.47, 0.32,0.12,0.32)

Now i would like to have a table which looks like this:

NaN   10001 10002 20001
10001 NaN   0.47  0.12
10002 0.47  NaN   0.32
20001 0.12  0.32  NaN

I actually don't care what values are in the place of "NaN".

I'm pretty new to R. Does anybody have a good solution for doing this? Regards, simoet

1

1 Answers

5
votes

You can use xtabs:

xtabs(maxSlope~Origin+Destin)
       Destin
Origin  10001 10002 20001
  10001  0.00  0.47  0.12
  10002  0.47  0.00  0.32
  20001  0.12  0.32  0.00