FAQ
Hello,
I am trying to convert list of pairwise distances to a distance matrix for
spatial analysis (kriging). For instance, I have something like this for
each pair pf points, and I want to convert it to a matrix:

point1 point2 distance
1 1 0
1 2 4
2 2 0
2 1 4

Please let me know if there is a function or method that can do this.

Thanks,
Christy

## Search Discussions

•  at Nov 10, 2008 at 12:27 am ⇧
what should your matrix look like? do you mean something like this?

p1=c(1,1,2,2)
p2=c(1,2,1,2)
d=c(0,4,4,0)

cbind(p1,p2,d)

d2=matrix(d,length(d)/2,length(d)/2)
d2

Cheers,
Daniel

-------------------------
cuncta stricte discussurus
-------------------------

-----Urspr?ngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von skinny c
Gesendet: Sunday, November 09, 2008 7:11 PM
An: r-help at r-project.org
Betreff: [R] how to convert indvidual pairwise distances to matrix

Hello,
I am trying to convert list of pairwise distances to a distance matrix for
spatial analysis (kriging). For instance, I have something like this for
each pair pf points, and I want to convert it to a matrix:

point1 point2 distance
1 1 0
1 2 4
2 2 0
2 1 4

Please let me know if there is a function or method that can do this.

Thanks,
Christy

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.
•  at Nov 10, 2008 at 12:29 am ⇧
Dear Christy,
Take a look at

http://www.nabble.com/Efficient-way-to-fill-a-matrix-to20351720.html#a20351720

Here are three options to do what you want:

# Data set
point1 point2 distance
1 1 0
1 2 4
2 2 0
closeAllConnections()

# Option 1
with(mydata,tapply(distance,list(point1,point2),"[[",1))

# Option 2
M <- matrix(,2,2)
attach(mydata)
M[cbind(point1,point2)]<-distance

# Option 3
library(reshape)
cast(mydata, point1~point2)

HTH,

Jorge

On Sun, Nov 9, 2008 at 7:11 PM, skinny c wrote:

Hello,
I am trying to convert list of pairwise distances to a distance matrix for
spatial analysis (kriging). For instance, I have something like this for
each pair pf points, and I want to convert it to a matrix:

point1 point2 distance
1 1 0
1 2 4
2 2 0
2 1 4

Please let me know if there is a function or method that can do this.

Thanks,
Christy

[[alternative HTML version deleted]]

______________________________________________
r-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

## Related Discussions

Discussion Overview
 group r-help categories r posted Nov 10, '08 at 12:11a active Nov 10, '08 at 12:29a posts 3 users 3 website r-project.org irc #r

### 3 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase