获取 edge_list 上两个顶点的度数索引和单独数据框列上的索引

Get out degree indice for both vertices on the edge_list and out the indices on separate dataframe columns

我使用 igraph 包中的 degree() 函数来计算边上两个顶点的度数索引,作为一个包含 7 个唯一边的小示例边列表,我想知道如何渲染对于同一唯一边上的两个顶点,这些出度索引分为两个单独的列,下面是我的示例代码:

library(igraph)
g <- graph.formula(1-2-3-4, 2-5, 3-6, 2-4-7)
degs <- degree(g, mode = "out")

所需的输出应如下所示

from to from_out to_out
1     2      1       4
2     3      4       3 
3     4      3       3 
2     5      4       1
3     6      3       1
2     4      4       3 
4     7      3       1

如果有人能对此有所说明,我们将不胜感激。

#turn graph to data.frame
DF <- as_data_frame(g)

#degs is a named vector
DF$from_out <- degs[as.character(DF$from)]
DF$to_out <- degs[as.character(DF$to)]
#  from to from_out to_out
#1    1  2        1      4
#2    2  3        4      3
#3    2  4        4      3
#4    2  5        4      1
#5    3  4        3      3
#6    3  6        3      1
#7    4  7        3      1