在 R 中的网络图中操作边缘列表
Manipulating edgelist in a network graph in R
我有一个 edgelist
,其中包含以下列 from
和 to
。这表示节点之间的边。
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" ...)
然后我创建了一个网络对象,转换为图形对象,以计算其中心性度量:
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
这给了我一个包含列 c(“from”、“to”、“centrality”)的数据框。但是,“from”和“to”不再是edgelist
中列出的原始节点名称。它们已经被转换成不同的ID,从1...到最后一行。
#my current results
from = c("1","2","3","4"...)
to = c("6", "100", "204", ...)
edge.betweenness.centrality = c(4653193, 20188105, ...)
如何合并回原来的节点名称?我需要确定实际的“从”和“到”(即节点数据),例如:
#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, ...)
我认为这可行 - 节点 ID 现在已恢复原状!
#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.)])
这是一个使用 igraph 的例子:
library(igraph)
我们创建一个具有明确顶点名称的示例图:
g <- sample_pa(length(letters), m=2, directed=F)
V(g)$name <- letters
计算边缘介数并将其保存到边缘属性:
E(g)$eb <- edge_betweenness(g)
现在您要求的数据框,原始顶点名称:
> 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
...
...
我有一个 edgelist
,其中包含以下列 from
和 to
。这表示节点之间的边。
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" ...)
然后我创建了一个网络对象,转换为图形对象,以计算其中心性度量:
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
这给了我一个包含列 c(“from”、“to”、“centrality”)的数据框。但是,“from”和“to”不再是edgelist
中列出的原始节点名称。它们已经被转换成不同的ID,从1...到最后一行。
#my current results
from = c("1","2","3","4"...)
to = c("6", "100", "204", ...)
edge.betweenness.centrality = c(4653193, 20188105, ...)
如何合并回原来的节点名称?我需要确定实际的“从”和“到”(即节点数据),例如:
#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, ...)
我认为这可行 - 节点 ID 现在已恢复原状!
#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.)])
这是一个使用 igraph 的例子:
library(igraph)
我们创建一个具有明确顶点名称的示例图:
g <- sample_pa(length(letters), m=2, directed=F)
V(g)$name <- letters
计算边缘介数并将其保存到边缘属性:
E(g)$eb <- edge_betweenness(g)
现在您要求的数据框,原始顶点名称:
> 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
...
...