-1

I'm new to R and this is issue is bothering me a lot. I have a weighted and directed network and I want to do the following:

I have an igraph network. I want to calculate the edge_betweenness of all edges and create a matrix with the following columns:

edgeID, node1, node2, weight, edgeBetweenness

By edgeID I mean the index of the edge in the graph. I need the index or ID because I want to use the elements of this matrix in another matrix.

So thanks for your help.

Gigitsu
  • 593
  • 6
  • 19
  • 3
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. Also try to edit the title to better represent what the specific issue is – camille Mar 01 '20 at 18:35

1 Answers1

1

First off, please consider camille's advice on how to provide a reproducible & minimal example. For future posts, it is always good to provide some sample data for us to work with.

In response to your question, let's generate a random sample graph and assign some random weights to every edge. I'm using a fixed random seed to ensure reproducibility of random data.

set.seed(2020)
ig <- graph.full(5)
E(ig)$weights <- sample(10, length(E(ig)), replace = T)

Then we can use igraph::as_data_frame and igraph::edge_betweenness to extract an edge list (including weights) and the edge betweenness, respectively.

transform(
    edgeID = 1:length(ig),
    as_data_frame(ig),
    edgeBetweenness = edge_betweenness(ig))
#    from to weights edgeID edgeBetweenness
# 1     1  2       7      1               1
# 2     1  3       6      2               1
# 3     1  4       8      3               1
# 4     1  5       1      4               1
# 5     2  3       1      5               1
# 6     2  4       4      6               1
# 7     2  5      10      7               1
# 8     3  4       6      8               1
# 9     3  5       1      9               1
# 10    4  5       8     10               1
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68