Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import weighted edgelist using igraph

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?

like image 907
user1723765 Avatar asked Oct 13 '12 15:10

user1723765


People also ask

What is the difference between weighted graph and igraph?

In weighted graphs, a real number is assigned to each (directed or undirected) edge. The input graph. In igraph edge weights are represented via an edge attribute, called ‘weight’.

What is the use of is_weighted function in igraph?

In igraph edge weights are represented via an edge attribute, called ‘weight’. The is_weighted function only checks that such an attribute exists. (It does not even checks that it is a numeric edge attribute.) Edge weights are used for different purposes by the different functions.

How do I add edges to an igraph?

You can add edges by calling Graph.add_edges () - but in order to add edges, you have to refer to existing vertices somehow. igraph uses integer vertex IDs starting from zero, thus the first vertex of your graph has index zero, the second vertex has index 1 and so on.

What is the best way to save an igraph graph?

Your best bet is probably GraphML or GML if you want to save igraph graphs in a format that can be read from an external package and you want to preserve numeric and string attributes. Edge list and NCOL is also fine if you don’t have attributes (NCOL supports vertex names and edge weights, though).


1 Answers

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
like image 54
user1317221_G Avatar answered Nov 09 '22 11:11

user1317221_G