[R] hi... problems about adjacency matrix

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Jul 8 14:56:57 CEST 2010


Hi Gina,

On Thu, Jul 8, 2010 at 6:33 AM, Gina Liao <yi713 at hotmail.com> wrote:
>
> Dear all,
> Hi, I have the problems about converting the matrix to adjacency matrix.Here's my example,
>          a            b               c                d                 e               fa 1.0000000 0.4048823682 0.1228531 0.49046991 0.4945158868 0.307443317b 0.4048824 1.0000000000 0.4367475 0.96949219 0.0007378596 0.560747765c 0.1228531 0.4367474719 1.0000000 0.40037341 0.3157538204 0.428183667d 0.4904699 0.9694921891 0.4003734 1.00000000 0.0661313658 0.575606129e 0.4945159 0.0007378596 0.3157538 0.06613137 1.0000000000 0.001076251f 0.3074433 0.5607477645 0.4281837 0.57560613 0.0010762514 1.000000000
> and my threshold value is 0.4763, which means any value above it is "1" and any values below it is "0".
> so my adjacency matrix should be like this,
>     a   b   c   d   e   fa   1   0   0   1   1   0b   0   1   0   1   0   1c   0   0   1   0   0   0d   1   1   0   1   0   1e   1   0   0   0   1   0f    0   1   0   1   0   1
>
> I'd like to ask how to convert the matrix to adjacency matrix.
> Thanks!!!

Just a note for future postings: use dput (?dput) to get your matrix
into a "pastable" format that we can use to copy into a working R
session and provide code.

I'll just make own random matrix for now that I'll turn into an
adjacency matrix:

R> set.seed(123)
R> real <- matrix(runif(30, 0, 1), nrow=6)
R> colnames(real) <- c('a', 'b', 'c', 'd', 'e')

(oops that has one less column than you)

Now I can use "ifelse" to convert to an adjacency matrix
R> adj <- ifelse(real > 0.4763, 1, 0)
R> adj
     a b c d e
[1,] 0 1 1 0 1
[2,] 1 1 1 1 1
[3,] 0 1 0 1 1
[4,] 1 0 1 1 1
[5,] 1 1 0 1 0
[6,] 0 0 0 1 0

The "ifelse" call could have been broken down into several steps. To
make it more clear:

Set up an "empty" adj matrix to fill w/ ones where appropriate
R> adj2 <- matrix(0, nrow=nrow(real), ncol=ncol(real))
R> colnames(adj2) <- colnames(real)

Now fill with ones:
R> adj2[real > 0.4763] <- 1
R> all(adj == adj2)
[1] TRUE

Also works.

And just to give you an idea of how to use dput:

R> dput(adj)
structure(c(1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), .Dim = c(6L, 5L), .Dimnames = list(
    NULL, c("a", "b", "c", "d", "e")))

Hope that helps.

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list