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

Peter Solymos solymos at ualberta.ca
Fri Jul 24 18:02:16 CEST 2009


No worries!

I just added the option 'dim.names = TRUE' by which you
can get the dimnames back, but you still need the
matrix > dist > data.frame coercion chain.

as.data.frame.dist <-
function (x, row.names = NULL, optional = FALSE, dim.names = FALSE, ...)
{
    if (!missing(optional))
        .NotYetUsed("optional", error = FALSE)
    id <- as.matrix(x)
    id[lower.tri(id)] <- 1
    id[upper.tri(id)] <- 0
    diag(id) <- 0
    rm <- row(id)
    cm <- col(id)
    rm <- array(rm)[array(id) == 1]
    cm <- array(cm)[array(id) == 1]
    out <- data.frame(row=rm, col=cm, dist=dist2vec(x))
    if (!is.null(row.names))
        rownames(out) <- row.names
    if (dim.names) {
        out$row <- as.factor(out$row)
        out$col <- as.factor(out$col)
        levels(out$row) <- rownames(id)
        levels(out$col) <- colnames(id)
    }
    out
}

dist2vec
function (x)
{
    attributes(x) <- NULL
    x
}

Cheers,

Peter



2009/7/24 Zhang Jinlong <zhangjl at ibcas.ac.cn>:
> Dear Peter,
>    Sorry for bothering you so much.
>    You are right,the function melt() in reshape package returns to full
> pairs of data. The method you write give exactly what I needed.There's
> another function, which was written by my friend Guofang, could solve the
> trouble(see below).
>    Thank you very much indeed.
>    Best Regards
>
>   Yours Jin
>
>
>
> read.tri.matrix=function(d)
> {
> if(is.null(colnames(d)))colnames(d)=paste('V',1:dim(d)[2],sep="")
> if(!is.matrix(d)) as.matrix(d)->d
> m=lower.tri(d) # 提取矩阵下三角
> colnames(m)=row.names(d)
> rownames(m)=row.names(d)
> as.data.frame(m)->m
> stack(m)->xxxx
> xxxx
> names(xxxx)[2]="contrast1"
> rep(colnames(d),dim(d)[1])->xxxx$contrast2
> d=as.data.frame(d)
> stack(d)$values->xxxx$value
> xxxx[xxxx$values,-1]->result
> row.names(result)=NULL
> result
> }
> # by Guofang LIU from IBCAS
>
>
>
>
>
>
>
>
>
>> 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
>
>> > 燬uppose I have an correlation matrix of four places named "A","B","C"and"D", as below:
>
>> > ?燗 ??B ??燙 ?燚
>
>> > A ?0 ??.5 ?0.2 ?0.8
>
>> > B ?.5 ?0 ??0.6 ?0.4
>
>> > C ?.2 ?0.6 ?0 ??0.4
>
>> > D ?.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
>
>> >
>
>> >
>
>>
>
> Jin-Long ZHANG
> Ph.D. Candidate
> Institute of Botany
> Chinese Academy of Sciences
> Beijing 100093
> E-mail: zhangjl at ibcas.ac.cn
>
>
>
>


More information about the R-sig-ecology mailing list