Re: [R] how to convert indvidual pairwise distances to matrix

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Sun, 09 Nov 2008 19:29:01 -0500

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
mydata=read.table(textConnection("
point1 point2 distance

1              1            0
1              2            4
2              2            0
2              1            4"),header=TRUE)
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 <skinny5555_at_gmail.com> 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_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 10 Nov 2008 - 00:36:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 10 Nov 2008 - 02:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive