首页 > 解决方案 > 从linkcomm包中检索二进制交互作为R中的数据框

问题描述

假设我有以下集群:

library(linkcomm)
g <- swiss[,3:4]
lc <-getLinkCommunities(g)
plot(lc, type = "members")
getNodesIn(lc, clusterids = c(3, 7, 8))

从图中您可以看到节点 6 存在于 3 个重叠的集群中:3、7 和 8。我很想知道如何将这些集群中的直接二进制交互作为数据框检索。具体来说,我想要一个数据框,其中集群 id 作为第一列,最后两列作为“交互器 1”和“交互器 2”,其中可以列出每个集群的所有交互器对。这些应该是直接的,即它们具有共同的优势。

基本上我想要这样的东西:

Cluster ID   Interactor 1  Interactor 2
3               6                14
3               3                7
3               6                7
3               14               3
3               6                3

以此类推其他ID。如果可能的话,我想避免重复,例如 6 和 14、14 和 6 等。

非常感谢,

阿比盖尔

标签: rdataframeigraphdata-manipulation

解决方案


您可能正在寻找edges. 注意:用于str(lc)检查您感兴趣的对象中包含的所有内容。

lc$edges
#    node1 node2 cluster
# 1     17    15       1
# 2     17     8       1
# 3     15     8       1
# 4     16    13       2
# 5     16    10       2
# 6     16    29       2
# 7     14     6       3
# 8 ...

res <- setNames(lc$edges, c(paste0("interactor.", 1:2), "cluster"))[c(3, 1, 2)]
res
#    cluster interactor.1 interactor.2
# 1        1           17           15
# 2        1           17            8
# 3        1           15            8
# 4        2           16           13
# 5        2           16           10
# 6        2           16           29
# 7        3           14            6
# 8 ...

推荐阅读