[英]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.