[英]How to determine number of vertices per cluster in R with igraph
通常,当我想确定图的顶点数时,我只需要在我的脚本中编写:
library(igraph)
vcount('name of your graph')
然后我就有了。
问题是我正在尝试确定每个集群(社区)的顶点数,但我不知道该怎么做。 igraph 中是否有任何 function 可以帮助我做到这一点?
到目前为止,这是我的代码:
library(igraphdata)
library(igraph)
data("UKfaculty")
newgraph <- as.undirected(UKfaculty)
cluster <- cluster_louvain(newgraph)
plot(newgraph, vertex.color=rainbow(5, alpha=0.6)[cluster$membership])
igraph 中是否有任何 function 可以帮助我确定每个集群的顶点数?
我认为这可能会做你想要的。 首先,将社区成员分配给节点:
V(newgraph)$community <- membership(cluster)
现在您可以基于社区制作子图并将vcount
应用于它:
vcount(induced_subgraph(newgraph, v = V(newgraph)$community == 1))
[1] 18
并使用sapply
获取所有 5 个社区的计数:
sapply(1:5, function(x) vcount(induced_subgraph(newgraph, v = V(newgraph)$community == x)))
[1] 18 19 27 6 11
试table
> table(cluster$membership)
1 2 3 4 5
18 19 27 6 11
为此,有一个名为sizes()
的 igraph function。 您可以使用sizes(cluster)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.