Skip to content

To transform an adjacency matrix

4 messages · Arnaud Michel, Pascal Oettli, David L Carlson

#
Hello,

One approach is:

m <- structure(c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 0, 0), .Dim = c(5L, 5L))
out <- which(m==1, arr.ind=TRUE)
out[order(out[,1]),]

Regards,
Pascal
On 20 November 2013 19:28, Arnaud Michel <michel.arnaud at cirad.fr> wrote:

  
    
#
Thank you Pascal
Its fine
Michel
Le 20/11/2013 11:55, Pascal Oettli a ?crit :

  
    
#
[,1] [,2]
[1,]    4    1
[2,]    2    3
[3,]    4    3
[4,]    2    4
[5,]    1    5
# If you want the result sorted
[,1] [,2]
[1,]    1    5
[2,]    2    3
[3,]    2    4
[4,]    4    1
[5,]    4    3

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Arnaud Michel
Sent: Wednesday, November 20, 2013 4:29 AM
To: R help
Subject: [R] To transform an adjacency matrix

Hi
I have the following problem
I would like to build, from a matrix filled with 0 and with 1, a
matrix 
or a data.frame which contains, in every line, the number of the
line 
and the number of the column of the matrix for which the value
is equal 
to 1.
Exemple :

dput(m)
structure(c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 0), .Dim = c(5L, 5L))

Result

1 5
2 3
2 4
4 1
4 3

Thank you for your help