Skip to content

Problem with a matrix

4 messages · Ambrosini Alessandro, Peter Dalgaard, Uwe Ligges

#
I have a scattered matrix that I have to import from an external file. In
this case all the lines could have a different size. The graph is oriented.
I give you an example:
a b c
b a c d
c b

The first row expresses that "a" has a direct path with "b" and also with
"c".
In the second "b" has a path with "a" and one with "c". In the third "c"
there is a direct path with "b".
I want to obtain the adjacency matrix.  In this case it is:
0 1 1 0
1 0 1 1
0 1 0 0
0 0 0 0

What have I to do?
Please help me. Excuse me for my english.
Alessandro Ambrosini

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Ambrosini Alessandro" <klavan at tiscalinet.it> writes:
[seen much worse]

Here you go:
[1] "a b c"   "b a c d" "c b"
[[1]]
[1] "a" "b" "c"

[[2]]
[1] "b" "a" "c" "d"

[[3]]
[1] "c" "b"
[[1]]
[1] 1 2 3

[[2]]
[1] 2 1 3 4

[[3]]
[1] 3 2
[[1]]
     [,1] [,2]
[1,]    1    2
[2,]    1    3

[[2]]
     [,1] [,2]
[1,]    2    1
[2,]    2    3
[3,]    2    4

[[3]]
     [,1] [,2]
[1,]    3    2
[,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    1
[4,]    2    3
[5,]    2    4
[6,]    3    2
[,1] [,2] [,3] [,4]
[1,]    0    1    1    0
[2,]    1    0    1    1
[3,]    0    1    0    0
[4,]    0    0    0    0
#
Thank you very much. Everything is ok.
I make you another question (and I hope that it will be the last one).
If names of nodes are not "a", "b", "c" but "Italy", "Spain", "England" how
can I change the command

 x <- lapply(x,match,letters) to obtain the usual matrix?
If I write the command in this way, the output is NA NA NA ... and so maybe
I have to change "letters". What have I to write?
Thank you and excuse me for the disturb.
Alessandro

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Ambrosini Alessandro wrote:
Instead of the
 letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
"r" "s" "t" "u" "v" "w" "x" "y" "z"
you might want to define and use a variable
 countries <- c("Italy", "Spain", "England")

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._