Skip to content
Prev 227559 / 398500 Next

Import graph object

On 14/07/2010 4:19 PM, Michael Haenlein wrote:
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