Dear all, I have a txt file of the following format that describes the relationships between a network of a certain number of nodes. {4, 2, 3} {3, 4, 1} {4, 2, 1} {2, 1, 3} {2, 3} {} {2, 5, 1} {3, 5, 4} {3, 4} {2, 5, 3} For example the first line {4, 2, 3} implies that there is a connection between Node 1 and Node 4, a connection between Node 1 and Node 2 and a connection between Node 1 and Node 3. The second line {3, 4, 1} implies that there is a connection between Node 2 and Node 3 as well as Node 4 and Node 1. Note that some of the nodes can be isolated (i.e., not have any connections to any other node) which is then indicated by {}. Also note that the elements in each row are not necessarily ordered (i.e., {4, 2, 3} instead of {2, 3, 4}). I would like to (a) read the txt file into R and (b) convert it to an adjacency matrix. For example the adjacency matrix corresponding to the aforementioned example is as follows: 0 1 1 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 Is there any convenient way of doing this? Thanks, Michael [[alternative HTML version deleted]]
On 14/07/2010 4:19 PM, Michael Haenlein wrote:> Dear all, > > I have a txt file of the following format that describes the relationships > between a network of a certain number of nodes. > > {4, 2, 3} > {3, 4, 1} > {4, 2, 1} > {2, 1, 3} > {2, 3} > {} > {2, 5, 1} > {3, 5, 4} > {3, 4} > {2, 5, 3} > > For example the first line {4, 2, 3} implies that there is a connection > between Node 1 and Node 4, a connection between Node 1 and Node 2 and a > connection between Node 1 and Node 3. The second line {3, 4, 1} implies that > there is a connection between Node 2 and Node 3 as well as Node 4 and Node > 1. Note that some of the nodes can be isolated (i.e., not have any > connections to any other node) which is then indicated by {}. Also note that > the elements in each row are not necessarily ordered (i.e., {4, 2, 3} > instead of {2, 3, 4}). I would like to (a) read the txt file into R and (b) > convert it to an adjacency matrix. For example the adjacency matrix > corresponding to the aforementioned example is as follows: > > 0 1 1 1 0 0 0 0 0 0 > 1 0 1 1 0 0 0 0 0 0 > 1 1 0 1 0 0 0 0 0 0 > 1 1 1 0 0 0 0 0 0 0 > 0 1 1 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 > 1 1 0 0 1 0 0 0 0 0 > 0 0 1 1 1 0 0 0 0 0 > 0 0 1 1 0 0 0 0 0 0 > 0 1 1 0 1 0 0 0 0 0 > > Is there any convenient way of doing this?It'll take a little work, but the general strategy is this: Use readLines to read the whole file, putting each line into one element of a character vector. Use a loop of some sort to proceed through the vector. Strip off the braces, use scan() to read the numbers, use the numbers to set the 1's in the adjacency matrix. For example (untested): x <- readLines( .. ) M <- matrix(0, length(x), length(x)) for (i in seq_along(x)) { y <- gsub("[{},]", " ", x[i]) entries <- scan(textConnection(y)) M[i, entries] <- 1 } This leaves a bunch of textConnections open so you could clean up by calling closeAllConnections (or close each one), but other than that it should work. Duncan Murdoch
On Jul 14, 2010, at 3:19 PM, Michael Haenlein wrote:> Dear all, > > I have a txt file of the following format that describes the relationships > between a network of a certain number of nodes. > > {4, 2, 3} > {3, 4, 1} > {4, 2, 1} > {2, 1, 3} > {2, 3} > {} > {2, 5, 1} > {3, 5, 4} > {3, 4} > {2, 5, 3} > > For example the first line {4, 2, 3} implies that there is a connection > between Node 1 and Node 4, a connection between Node 1 and Node 2 and a > connection between Node 1 and Node 3. The second line {3, 4, 1} implies that > there is a connection between Node 2 and Node 3 as well as Node 4 and Node > 1. Note that some of the nodes can be isolated (i.e., not have any > connections to any other node) which is then indicated by {}. Also note that > the elements in each row are not necessarily ordered (i.e., {4, 2, 3} > instead of {2, 3, 4}). I would like to (a) read the txt file into R and (b) > convert it to an adjacency matrix. For example the adjacency matrix > corresponding to the aforementioned example is as follows: > > 0 1 1 1 0 0 0 0 0 0 > 1 0 1 1 0 0 0 0 0 0 > 1 1 0 1 0 0 0 0 0 0 > 1 1 1 0 0 0 0 0 0 0 > 0 1 1 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 > 1 1 0 0 1 0 0 0 0 0 > 0 0 1 1 1 0 0 0 0 0 > 0 0 1 1 0 0 0 0 0 0 > 0 1 1 0 1 0 0 0 0 0 > > Is there any convenient way of doing this? > > Thanks, > > MichaelRead the file in with readLines(): # I am on OSX, so copied from the clipboard Lines <- readLines(pipe("pbpaste"))> Lines[1] "{4, 2, 3}" "{3, 4, 1}" "{4, 2, 1}" "{2, 1, 3}" "{2, 3}" [6] "{}" "{2, 5, 1}" "{3, 5, 4}" "{3, 4}" "{2, 5, 3}" # parse the numbers from Lines L.split <- strsplit(Lines, split = "[{},]")> L.split[[1]] [1] "" "4" " 2" " 3" [[2]] [1] "" "3" " 4" " 1" [[3]] [1] "" "4" " 2" " 1" [[4]] [1] "" "2" " 1" " 3" [[5]] [1] "" "2" " 3" [[6]] [1] "" "" [[7]] [1] "" "2" " 5" " 1" [[8]] [1] "" "3" " 5" " 4" [[9]] [1] "" "3" " 4" [[10]] [1] "" "2" " 5" " 3" # Create an initial square matrix of 0's mat <- matrix(0, length(Lines), length(Lines))> mat[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 0 0 [5,] 0 0 0 0 0 0 0 0 0 0 [6,] 0 0 0 0 0 0 0 0 0 0 [7,] 0 0 0 0 0 0 0 0 0 0 [8,] 0 0 0 0 0 0 0 0 0 0 [9,] 0 0 0 0 0 0 0 0 0 0 [10,] 0 0 0 0 0 0 0 0 0 0 # Set the positions in each row to 1 for (i in seq(along = L.split)) mat[i, as.numeric(L.split[[i]])] <- 1> mat[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 1 1 1 0 0 0 0 0 0 [2,] 1 0 1 1 0 0 0 0 0 0 [3,] 1 1 0 1 0 0 0 0 0 0 [4,] 1 1 1 0 0 0 0 0 0 0 [5,] 0 1 1 0 0 0 0 0 0 0 [6,] 0 0 0 0 0 0 0 0 0 0 [7,] 1 1 0 0 1 0 0 0 0 0 [8,] 0 0 1 1 1 0 0 0 0 0 [9,] 0 0 1 1 0 0 0 0 0 0 [10,] 0 1 1 0 1 0 0 0 0 0 HTH, Marc Schwartz