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

______________________________________________
R-help@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.

Reply via email to