我有一個網路/圖 ( test_FINAL),我想從中洗掉下面的一組頂點:
vertex <- c(1, 3, 7, 9, 10)
這個想法是在clustering analyses洗掉頂點后運行 a 。例如,在第一次迭代中,應該洗掉頂點#1,然后洗掉頂點#1 和#3,以及隨后的#1、#3、#7 等等,直到最后一次迭代中所有頂點都被洗掉并進行聚類執行。
我的代碼如下所示:
# Library
library(igraph)
# Create data
set.seed(1)
data <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.8,0.2)), nc=10)
network <- graph_from_adjacency_matrix(data , mode='undirected', diag=F )
# Default network
par(mar=c(0,0,0,0))
plot(network)
vertex <- c(1, 3, 7, 9, 10)
# Estimating cluster statistics *prior to* removing vertices
pre_cluster <- cluster_fast_greedy(network)
length(pre_cluster); sizes(pre_cluster); modularity(pre_cluster)
# removing vertices
final_graph <- delete_vertices(network, c(vertex))
cluster_graph <- cluster_fast_greedy(final_graph)
# Estimating cluster statistics *after* removing vertices
length(cluster_graph); sizes(cluster_graph); modularity(cluster_graph)
在 a 中執行此操作的最佳方法是什么loop,尤其是對于all the vertices洗掉然后估計集群統計資訊的最終迭代?
uj5u.com熱心網友回復:
下面怎么樣
vertex <- c(1, 3, 7, 9, 10)
cumulator <- vertex[1]
while (cumulator <= length(vertex)) {
to_be_deleted <- vertex[c(1:cumulator)]
cumulator = cumulator 1
print(to_be_deleted)
}
然后在回圈的每次迭代中while,您都可以運行洗掉功能。
uj5u.com熱心網友回復:
可能您可以從使用Reduce開始創建頂點的累積串列
> Reduce(c, vertex, accumulate = TRUE)
[[1]]
[1] 1
[[2]]
[1] 1 3
[[3]]
[1] 1 3 7
[[4]]
[1] 1 3 7 9
[[5]]
[1] 1 3 7 9 10
然后嘗試
lapply(
Reduce(c, vertex, accumulate = TRUE),
function(x) {
cluster_fast_greedy(
delete_vertices(
network,
x
)
)
}
)
這使
[[1]]
IGRAPH clustering fast greedy, groups: 3, mod: 0.32
groups:
$`1`
[1] 2 6 8 9
$`2`
[1] 1 5 7
$`3`
[1] 3 4
[[2]]
IGRAPH clustering fast greedy, groups: 3, mod: 0.29
groups:
$`1`
[1] 5 7 8
$`2`
[1] 1 4 6
$`3`
[1] 2 3
[[3]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.36
groups:
$`1`
[1] 2 3 6 7
$`2`
[1] 1 4 5
[[4]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.32
groups:
$`1`
[1] 2 3 6
$`2`
[1] 1 4 5
[[5]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.38
groups:
$`1`
[1] 1 4 5
$`2`
[1] 2 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492626.html
上一篇:在最后6行中找到非連續增加-R中for回圈的替代方案?
下一篇:回圈遍歷集合并組合元素
