[R] To transform an adjacency matrix

David Carlson dcarlson at tamu.edu
Wed Nov 20 16:38:39 CET 2013


> indx <- arrayInd(which(m>0), .dim=c(5, 5))
> indx
     [,1] [,2]
[1,]    4    1
[2,]    2    3
[3,]    4    3
[4,]    2    4
[5,]    1    5
# If you want the result sorted
> indx[order(indx[,1], indx[,2]),]
     [,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

-- 
Michel ARNAUD
Chargi de mission auprhs du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31


	[[alternative HTML version deleted]]



More information about the R-help mailing list