Retrieving binary interactions from linkcomm package as a data frame in R

﹥>﹥吖頭↗ 提交于 2020-06-17 00:06:30

问题


Suppose I have the following clusters:

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

From the plot you can see the node 6 is present in 3 overlapping clusters: 3, 7 and 8. I am interested to know how to retrieve the direct binary interactions in these clusters as a data frame. Specifically, I would like a data frame with the cluster id as the first column, and the last two columns as "interactor 1" and "interactor 2", where all pairs of interactors can be listed per cluster. These should be direct, i.e. they have an edge in common.

Basically I would like something like this:

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

and so on for the other ids. If possible I would like to avoid duplicates such as 6 and 14, 14 and 6 etc.

Many thanks,

Abigail


回答1:


You might be looking for the edges. Note: Use str(lc) to examine what's all included in your object of interest.

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 ...


来源:https://stackoverflow.com/questions/61897539/retrieving-binary-interactions-from-linkcomm-package-as-a-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!