Jasmina - 1 year ago 74

R Question

I have a graph object that I would like to traverse so I can get edge connectivity readings for every node with the edge_connectivity function in igraph. The output should look like this:

edge_connectivity(di, "10001", "12256")

[1] 2

edge_connectivity(di, "10001", "8496")

[1] 2

edge_connectivity(di, "10001", "8558")

[1] 2

*node names are randomly generated integers

I can't get the lapply function to work properly, though. Here is the code I've been trying to accomplish this with:

`library(igraph)`

digraph<-read.csv('1999.csv')

di<-graph.data.frame(digraph)

edge_connectivity<-lapply(di, function(x) x)

I know this is not right. Should it be something like:

`connectivity<-lapply(V(di), function(x) length(edge_connectivity(di))`

But it is not correct either, since the edge_connectivity function has to either have source and target arguments or be left blank for the entire network. I just don't know how to make it traverse the network on its own. Any help will be greatly appreciated! I apologize for my ignorance...

J.

Answer Source

Since you want to get the edge connectivity score for each pair of nodes in the network, you need to find all the combinations of pairs and pass this to `sapply`

along with the function `edge_connectivity`

. I think you want `sapply`

here (which returns a vector) and not `lapply`

(which returns a list). The syntax is the same in either case.

Using random graph 'g', find all the possible pairs of nodes with `combn`

. This creates a Nx2 matrix, where N is the total number of possible pairs.

```
g <- sample_gnp(25,0.5,directed=T)a
c <- combn(V(g),2)
```

Tell `sapply`

to apply the `ege_connectivity`

function over the number of possible pairs of nodes (e.g. the number of columns in c). Index the first and second row in each column to get the each of the nodes in the pair.

```
sapply(1:ncol(c), function(x) edge_connectivity(g,c[1,x],c[2,x]))
```

This will return the connectivity score for each pair of nodes in the graph. Note the syntax of the `sapply`

function. The function is applied to each element in the first argument (`1:ncol(c)`

), where the element replaces each instances of `x`

in the function.