*Feb 9, 2013: Codes have been updated for igraph v0.6, R2.15.
Here, I'll deal with just one topic: using edgelists with associated edge values to create a weighted network.
Let's say you have an edgelist with associated values, like this:
This is actually the same data as the weighted adjacency matrix on a different page.
If this network is undirected, then it doesn't matter which vertex is in the 'V1' versus 'V2' columns. However, if the network is directed, then the convention is that the edge goes FROM V1 TO V2.
Also note that each V1-->V2 combo need to be unique. If you have a separate weight for V2-->V1 (e.g. 23871-->58009 is 1 but 58009-->23871 is 3), that's ok. However, if you have two rows that have the same V1-->V2 combo (e.g., 23871-->58009 is 1, and later there is another row that is 23871-->58009 = 3), this will be a problem. So, you either have to sum-up the weights of all duplicate vertex sets before hand, or you need to have each interaction as separate rows (see below). You could, of course, write your own codes to convert whatever format you have to an adjacency matrix to avoid these subtle complications.
The above data is available as a .csv file below ('el.with.weights.csv').
Converting this data into an network is pretty easy. Just remember that if your vertex IDs are numbers like mine, you need to designate them as characters or else igraph/statnet will get confused.
I'll use igraph for this example. Basically all you need to do is create a network using the first two columns using the graph.edgelist() function. Then, you add in the edge weights (third column) to this network:
**OR, a much easier way to import a weighted edgelist is to just use the graph.data.frame() function:
... because the third column is called "weight", this function adds an edge attribute called "weight" when creating the graph. You can have additional edge attributes as the 4th, 5th, etc. columns.
Note that the default for graph.edgelist() is to create a directed network, so the plot will look like this:
To get the weighted adjacency matrix from the network:
This is the "sparse" display of the adjacency matrix. Zeros are displayed as periods so that you can visually pick out the non-zero values. If you don't like this, you can use:
23732 23871 58098 23778 23824 58009 58256
23732 0 1 10 0 0 0 0
23871 0 0 5 15 0 1 0
58098 0 0 0 1 0 0 0
23778 8 0 0 0 1 0 0
23824 0 0 0 0 0 0 0
58009 0 0 7 0 0 0 0
58256 0 0 1 0 0 0 0
If you want the undirected network, then you simply use g=graph.edgelist(el[,1:2],directed=FALSE)
when importing the data, and the rest is the same, like so:
Then, the adjacency matrix you get from the network should be the same as those presented in the 'plotting weighted network' page (albeit with different ordering of vertex IDs).