将边权重分配给R中的igraph对象

时间:2015-11-19 20:32:18

标签: r graph igraph

我有一个共生矩阵,我想将其转换为igraph对象。矩阵有三列 - node1node2freq

我使用graph_from_edgelist命令创建了图表。

g <- graph_from_edgelist(as.matrix(coOccurDf[1:n,1:2]), directed=F)

我现在需要为边缘分配权重。我试图使用两个功能,这两个功能似乎都做同样的工作,但无济于事。

set.edge.attribute(g, "weight", index=E(g), coOccurDf[1:n,]$freq)

set_edge_attr(g, "weight", index=E(g), coOccurDf[1:n,]$freq)

这两个命令都不会引发错误,但是当我尝试使用

查看权重时
E(g)$weight

它只显示NULL

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以按以下方式返回igraph对象的权重:

edge.attributes(g)$weight

E(g)$weight

您可以通过分配以下任一对象来设置igraph对象的权重:

edge.attributes(g)$weight <- coOccurDf[1:n,]$freq

E(g)$weight <- coOccurDf[1:n,]$freq

要使用set.edge.attribute函数,它将返回分配了权重的新图形。要将其分配给对象g而不是将加权图输出到控制台:

g <- set.edge.attribute(g, "weight", index=E(g), coOccurDf[1:n,]$freq)
相关问题