Home > OS >  Manipulating edgelist in a network graph in R
Manipulating edgelist in a network graph in R

Time:10-24

I have an edgelist that has the following columns, from and to. This represents the edges between nodes.

from = c("10009", "10009",  "10009", "10009",  "10011", "10011", ...)
to = c("23908", "230908", "230908", "230908", "230514", "230514", ...)

edgelist = data.frame(from, to)
nodes = c("10009", "10011", "230908", "230514" ...)

I then created a network object, converted to graph object, to calculate its centrality measures:

library(network)
library(qgraph)
library(igraph)

network_el = network(edgelist, vertex.attr = nodes, directed=T) #network object

g = asIgraph(network_el) #convert to graph object

centrality = centrality_auto(g) #calculate Centrality

df = data.frame(centrality$edge.betweenness.centrality) #extract edge betweenness centrality into a dataframe

This gives me a dataframe with the columns c("from", "to", "centrality"). However, the "from" and "to" are no longer the original node names listed in edgelist. They have been converted into a different ID, starting from 1...to the last row.

#my current results
from = c("1","2","3","4"...)
to = c("6", "100", "204", ...)
edge.betweenness.centrality = c(4653193, 20188105, ...)

How do I merge back the original node names? I need to identify the actual "from" and "to" (i.e., the node data), such as:

#my desired results
from = c("10009", "10009", "10009", "10009", "10011"...) #rather than 1,2,3..
to = c("23908", "230908", "230908", "230908", "235014",...)
edge.betweenness.centrality = c(4653193, 20188105, ...)

CodePudding user response:

I think this works - the node IDs are now back in place!

#Assign as dictionary
dict <- data.frame(name = V(g)$vertex.names)
dict <- data.frame (row.names(dict), dict)

#Replace with dictionary values
df$from <- with(dict, name[match(df$from, row.names.dict.)])
df$to <- with(dict, name[match(df$to, row.names.dict.)])

CodePudding user response:

Here's an example using igraph:

library(igraph)

We create an example graph with explicit vertex names:

g <- sample_pa(length(letters), m=2, directed=F)
V(g)$name <- letters

Compute edge betweenness and save it to an edge attribute:

E(g)$eb <- edge_betweenness(g)

Now the dataframe you asked for, with the original vertex names:

> as_data_frame(g)
   from to        eb
1     a  b 12.831818
2     a  c 16.524242
3     b  c 25.700000
4     c  d 30.812879
5     b  d 12.464394
...
...
  • Related