[R] Compare two distance matrices
Martin Maechler
maechler at stat.math.ethz.ch
Thu Oct 6 18:10:33 CEST 2005
>>>>> "bady" == bady <bady at univ-lyon1.fr>
>>>>> on Thu, 06 Oct 2005 14:39:27 +0200 writes:
bady> Hi, hi all,
>> I am trying to compare two distance matrices with R. I would like to
>> create a XY plot of these matrices and do some linear regression on
>> it. But, I am a bit new to R, so i have a few questions (I searched in
>> the documentation with no success).
>> The first problem is loading a distance matrix into R. This matrix is
>> the output of a the Phylip program Protdist and lookes like this:
>> I tried with the scan() function to load the files, but with no
>> success. How should i load in these files? ....
>>
bady> you can separately load each matrix with two text files.
bady> require(ade4)
bady> mat1 <- read.table("mat1.txt")
bady> nam1 <- mat1[,1]
bady> mat1 <- mat1[,-1]
bady> row.names(mat1) <- names(mat1) <- nam1
bady> mat2 <- read.table("mat2.txt")
bady> nam2 <- mat2[,1]
bady> mat2 <- mat2[,-1]
bady> row.names(mat2) <- names(mat2) <- nam2
bady> dist1 <- mat2dist(mat1)
bady> dist2 <- mat2dist(mat2)
but I don't see why you would need an extra package "ade4" and
its "extra - function" mat2dist().
when the 'stats' package already provides the function
as.dist(.) {the help page of which was mentioned by the
original poster}.
Here is a reproducible example showing how I think as.dist()
works sufficiently:
> (m <- toeplitz(round(rnorm(6),2)))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.42 -0.78 -0.42 -2.24 0.74 1.31
[2,] -0.78 -0.42 -0.78 -0.42 -2.24 0.74
[3,] -0.42 -0.78 -0.42 -0.78 -0.42 -2.24
[4,] -2.24 -0.42 -0.78 -0.42 -0.78 -0.42
[5,] 0.74 -2.24 -0.42 -0.78 -0.42 -0.78
[6,] 1.31 0.74 -2.24 -0.42 -0.78 -0.42
> as.dist(m)
1 2 3 4 5
2 -0.78
3 -0.42 -0.78
4 -2.24 -0.42 -0.78
5 0.74 -2.24 -0.42 -0.78
6 1.31 0.74 -2.24 -0.42 -0.78
> ## it also works for data frames {if really needed}:
> dm <- as.data.frame(m)
> as.dist(dm)
1 2 3 4 5
2 -0.78
3 -0.42 -0.78
4 -2.24 -0.42 -0.78
5 0.74 -2.24 -0.42 -0.78
6 1.31 0.74 -2.24 -0.42 -0.78
>
More information about the R-help
mailing list