Import weighted edgelist using igraph

10k views Asked by At

I have the following txt file representing a network in edgelist format.

The first two columns represent the usual: which node is connected to which other nodes

The third column represents weights, representing the number of times each node has contacted the other.

I have searched the igraph documentation but there's no mention of how to include an argument for weight when importing standard file formats like txt.

The file can be accessed from here and this is the code I've been using:

read.graph("Irvine/OClinks_w.txt", format="edgelist")

This code treats the third column as something other than weight.

Does anyone know the solution?

2

There are 2 answers

0
user1317221_G On

does the following cause too much annoyance?

g <- read.table("Irvine/OClinks_w.txt")
g <- graph.data.frame(g)

if it does then directly from the file you can use

g<-read.graph("Irvine/OClinks_w.txt",format="ncol")
E(g)$weight
2
user217558 On

If you are using Python and igraph, the following line of code works to import weights and vertex names:

g1w=Graph.Read_Ncol("g1_ncol_format_weighted.txt",names=True)

Note: you must tell igraph to read names attribute with names=True, otherwise just vertex numbers will be imported.

Where g1_ncol_format_weighted.txt looks something like:

A B 2
B C 3

To make sure the import worked properly, use the following lines:

print(g1w.get_edgelist())
print(g1w.es["weight"])
print(g1w.vs["name"])