简体   繁体   English

如何使用 igraph 确定 R 中每个簇的顶点数

[英]How to determine number of vertices per cluster in R with igraph

normaly when I want to determine the number of vertices for a graph I only need to write in my script:通常,当我想确定图的顶点数时,我只需要在我的脚本中编写:

library(igraph)
vcount('name of your graph')

And then I have it.然后我就有了。

The thing is that I'm trying to determine the number vertices for each cluster (community) and I have no idea how to do it.问题是我正在尝试确定每个集群(社区)的顶点数,但我不知道该怎么做。 Is there any function in igraph that could help me to do it? igraph 中是否有任何 function 可以帮助我做到这一点?

Here's my code so far:到目前为止,这是我的代码:

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])

在此处输入图像描述

Is there any function in igraph that could help me to determine the number of vertices per cluster? igraph 中是否有任何 function 可以帮助我确定每个集群的顶点数?

I think this may do what you want.我认为这可能会做你想要的。 First, assign the community membership to the nodes:首先,将社区成员分配给节点:

V(newgraph)$community <- membership(cluster)

Now you can make a subgraph based on community and apply vcount to it:现在您可以基于社区制作子图并将vcount应用于它:

vcount(induced_subgraph(newgraph, v = V(newgraph)$community == 1))
[1] 18

And use sapply to get the count for all 5 communities:并使用sapply获取所有 5 个社区的计数:

sapply(1:5, function(x) vcount(induced_subgraph(newgraph, v = V(newgraph)$community == x)))
[1] 18 19 27  6 11

Try tabletable

> table(cluster$membership)

 1  2  3  4  5
18 19 27  6 11 

There is an igraph function called sizes() for this.为此,有一个名为sizes()的 igraph function。 You can use sizes(cluster) .您可以使用sizes(cluster)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM