Good afternoon.
I have 2 txt files. One containing one column the nodes and the other links( multiple columns separated by spaces containing 0(no links) and 1(directed links). I do not have any kind of headers in the files.
I want to import in R and transform them in graph object.
Example of node file.(column 1)
135
246
358
....
Example of links file.(multiple columns)
0 0 0 1 0
1 0 1 0 0
0 0 0 0 0
...........................
I tried https://kateto.net/network-visualization but with no success.
I convert them in Excel but no success.
Please I need you help...
Here is a way of creating a graph from two files, one with the nodes names and the other with the adjacency matrix.
Suppose the files names are as follows:
nodesfile <- "nodes.txt"
linksfile <- "links.txt"
Since they are to be read in as a vector and as a matrix, respectively, function scan
can be used.
nodes <- scan(file = nodesfile, what = character())
links <- scan(file = linksfile)
Now first coerce the vector links
above to a matrix and then create the graph. The code below assumes the graph is directed, see help("graph_from_adjacency_matrix")
.
library(igraph)
links <- matrix(links,
nrow = length(nodes),
byrow = TRUE,
dimnames = list(nodes, nodes))
g <- graph_from_adjacency_matrix(links)
plot(g)