查找R中igraph网络中节点之间的连接数

时间:2016-03-29 12:05:19

标签: r igraph

我创建了一个带有以下边缘的无向igraph A--AA--BB--AC--DA--CA--B

library(igraph)
my_data = data.frame(id1=c("A","A","B","C","A","A"),id2=c("A","B","A","D","C","B"))
g = graph.data.frame(my_data,directed=F)

我想要一个输出,它列出了每条边的所有唯一边和数。以下是基于上图的示例。

Column1 Column2 Column3
   A       A       1
   A       B       3
   A       C       1
   C       D       1

2 个答案:

答案 0 :(得分:1)

你可以做点什么,

table(vapply(paste(my_data$id1, my_data$id2), function(i) paste(sort(strsplit(i, ' ')[[1]]), collapse=' '), ' '))

#A A A B A C C D 
#  1   3   1   1 

答案 1 :(得分:0)

想通了。使用以下代码获取输出。

library(igraph)
require(splitstackshape)
require(data.table)

my_data = data.frame(id1=c("A","B","C","A","A"),id2=c("B","A","D","C","B"))
g = graph.data.frame(my_data,directed=F)

edge=as_ids(E(g))
edge=as.data.frame(edge)
split = as.data.table(cSplit(edge,"edge","|"))
split[,':='(dummy=1,
            key = paste(edge_1,edge_2,sep="_"))]
split = unique(split[,.(edge_1,edge_2,sum(dummy)),by=key])[,2:4,with=F]
split

# A B 3
# C D 1
# A C 1