使用 Edgelist 从二部图构建附属网络

Using Edgelist to construct affiliation networks from a bipartite graph

我有一个代表二分网络的边缘列表。

ID1 ID2
  1 123
  1 124
  1 125
  2 123
  2 126
  3 127
  3 123
  3 130

ID1 是 class 个演员,ID2 是另一个 class 个演员。如何为演员 classes 分配属性(ID1 中的所有顶点作为一个 class,ID2 中的所有顶点作为另一个 class)以将其用于二分网络分析。另外,我想从二分图中提取隶属关系网络作为两个单独的图并导出它们。

可以在此处找到重现数据帧的代码

x<-as.data.frame(matrix(c(1,1,1,2,2,3,3,3,123,124,125,123,126,127,123,130),8,2))
colnames(x) <- c("ID1", "ID2")
g <- graph.data.frame(x, directed=F)

提前致谢!

您基本上只需要使用 data.frame 中的数据为顶点分配一个类型属性。例如

is_bipartite(g)
# [1] FALSE
V(g)$type <- V(g)$name %in% x[["ID1"]]
is_bipartite(g)
# [1] TRUE

此代码使用顶点名称将第一列中的所有顶点设置为 TRUE。