### 09. Edgelists with edge values

*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:

 V1 V2 weight 23732 23871 1 23732 58098 10 23778 23732 8 23778 23824 1 23871 23778 15 23871 58009 1 23871 58098 5 58009 58098 7 58098 23778 1 58256 58098 1

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:

`el=read.csv(file.choose()) # read  the 'el.with.weights.csv' file`
```el[,1]=as.character(el[,1]) #Because the vertex IDs in this dataset are numbers, we make sure igraph knows these should be treated as characters. Otherwise, it'll create problems (see page on data import)```
`el[,2]=as.character(el[,2])`
`el=as.matrix(el) #igraph needs the edgelist to be in matrix format`
`g=graph.edgelist(el[,1:2]) #We first greate a network from the first two columns, which has the list of vertices`
`E(g)\$weight=as.numeric(el[,3]) #We then add the edge weights to this network by assigning an edge attribute called 'weight'. `

**OR, a much easier way to import a weighted edgelist is to just use the graph.data.frame() function:

`el=read.csv(file.choose()) # read the 'el.with.weights.csv' file `
`g2=graph.data.frame(el)`
... 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:

`plot(g,layout=layout.fruchterman.reingold,edge.width=E(g)\$weight/2)`

To get the weighted adjacency matrix from the network:
`adj=get.adjacency(g,attr='weight') #attr='weight' makes sure that the weights are shown in the adjacency matrix.`
`adj`

> adj
7 x 7 sparse Matrix of class "dgCMatrix"
23732 23871 58098 23778 23824 58009 58256
23732     .     1    10     .     .     .     .
23871     .     .     5    15     .     1     .
58098     .     .     .     1     .     .     .
23778     8     .     .     .     1     .     .
23824     .     .     .     .     .     .     .
58009     .     .     7     .     .     .     .
58256     .     .     1     .     .     .     .

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:

`adj=get.adjacency(g,attr='weight',sparse=FALSE) `
`adj`

> adj
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:

`el=read.csv(file.choose()) # read  the 'el.with.weights.csv' file`
`el[,1]=as.character(el[,1]) `
`el[,2]=as.character(el[,2])`
`el=as.matrix(el)`
`g=graph.edgelist(el[,1:2],directed=FALSE)`
`E(g)\$weight=as.numeric(el[,3]) plot(g,layout=layout.fruchterman.reingold,edge.width=E(g)\$weight)`

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).

 23732 23871 58098 23778 23824 58009 58256 23732 0 1 10 8 0 0 0 23871 1 0 5 15 0 1 0 58098 10 5 0 1 0 7 1 23778 8 15 1 0 1 0 0 23824 0 0 0 1 0 0 0 58009 0 1 7 0 0 0 0 58256 0 0 1 0 0 0 0

ċ
Dai Shizuka,
Feb 24, 2012, 12:31 PM
Comments