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:> 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.[seen much worse] Here you go:>x <- readLines("lines.txt") > x[1] "a b c" "b a c d" "c b"> x <- strsplit(x," ") > x[[1]] [1] "a" "b" "c" [[2]] [1] "b" "a" "c" "d" [[3]] [1] "c" "b"> x <- lapply(x,match,letters) > x[[1]] [1] 1 2 3 [[2]] [1] 2 1 3 4 [[3]] [1] 3 2> x <- lapply(x, function(z) cbind(z[1],z[-1])) > x[[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> x <- do.call("rbind", x) > x[,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 2 1 [4,] 2 3 [5,] 2 4 [6,] 3 2> m <- matrix(0,4,4) > m[x] <- 1 > m[,1] [,2] [,3] [,4] [1,] 0 1 1 0 [2,] 1 0 1 1 [3,] 0 1 0 0 [4,] 0 0 0 0 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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:> > 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.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._