[R] To transform an adjacency matrix
Pascal Oettli
kridox at ymail.com
Wed Nov 20 11:55:44 CET 2013
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:
> 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
> Chargé de mission auprès 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]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan
More information about the R-help
mailing list