我使用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
如果有人能对此有所了解,我将不胜感激。
发布于 2020-02-25 19:58:34
#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 1https://stackoverflow.com/questions/60393982
复制相似问题