[R-sig-eco] How to convert matrix to paired list?

Peter Solymos solymos at ualberta.ca
Fri Jul 24 15:47:48 CEST 2009


Hi,

The 'as.data.frame.dist' function requires the mefa package,
that's why I wrote the line 'library(mefa)'. This returns the
lower triangle only, but it does not return the row/col names.

The melt method, however returns the full matrix, not only
the lower triangle, see below.

Best,

Peter

--

x <- cbind(rnorm(10), rnorm(10), rnorm(10), rnorm(10))
colnames(x) <- LETTERS[1:4]
y <- cor(x)

> x <- cbind(rnorm(10), rnorm(10), rnorm(10), rnorm(10))
> colnames(x) <- LETTERS[1:4]
> y <- cor(x)
> y
            A           B          C          D
A  1.00000000 -0.01444285  0.3969146  0.1356350
B -0.01444285  1.00000000 -0.6151891  0.6649487
C  0.39691455 -0.61518910  1.0000000 -0.3847042
D  0.13563501  0.66494872 -0.3847042  1.0000000
> library(mefa)
> as.data.frame(as.dist(y))
  row col        dist
1   2   1 -0.01444285
2   3   1  0.39691455
3   4   1  0.13563501
4   3   2 -0.61518910
5   4   2  0.66494872
6   4   3 -0.38470419
> library(reshape)
> melt(y)
   X1 X2       value
1   A  A  1.00000000
2   B  A -0.01444285
3   C  A  0.39691455
4   D  A  0.13563501
5   A  B -0.01444285
6   B  B  1.00000000
7   C  B -0.61518910
8   D  B  0.66494872
9   A  C  0.39691455
10  B  C -0.61518910
11  C  C  1.00000000
12  D  C -0.38470419
13  A  D  0.13563501
14  B  D  0.66494872
15  C  D -0.38470419
16  D  D  1.00000000

On Thu, Jul 23, 2009 at 4:42 PM, Zhang Jinlong<zhangjl at ibcas.ac.cn> wrote:
> How to convert matrix to paired list?
> Dear lists
>  Suppose I have an correlation matrix of four places named "A","B","C"and"D", as below:
>    A     B      C    D
> A   0    0.5   0.2   0.8
> B  0.5   0     0.6   0.4
> C  0.2   0.6   0     0.4
> D  0.8   0.4   0.4   0
> Is there a function that allows me to generate the paired list like this?
> A B 0.5
> A C 0.2
> A D 0.8
> B C 0.6
> B D 0.4
> C D 0.4
> Could you help me if you have an method. Any suggestions will be appreciated.
> Thanks
>
> Jinlong
>
>
> Jin-Long ZHANG
> Ph.D. Candidate
> Institute of Botany
> Chinese Academy of Sciences
> Beijing 100093
> E-mail: zhangjl at ibcas.ac.cn
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
>



More information about the R-sig-ecology mailing list