我目前有这样的代码:
library(igraph)
set.seed(123); g <- erdos.renyi.game(30, 151 , type = "gnm" , directed = F , loops = F) %>%
set_vertex_attr("a", value = 0) %>%
set_vertex_attr("b", value = 0)
V(g)$b <- sample(c(0, .6, .7), vcount(g), replace = TRUE, prob = c(0.3, .4, .3))
repeat{
mean <- mean(V(g)$a == 1)
prev_value <- mean(v(g)$a == 1)
V(g)$a[V(g)$b <= mean & V(g)$a == 0] <- 1
curr_value <- mean(v(g)$a == 1)
if(prev_value == curr_value){
break
}
}
我想在if代码中添加一些东西,随机删除具有特定&#34; trait&#34;的边缘。在这种情况下,我希望特征是两个连接的节点的值都为0,对于&#34; a&#34;。
答案 0 :(得分:0)
我想[...]随机删除两个连接的边缘[where] 对于&#34; a&#34;。
,节点的值均为0
你可以尝试
# trait:
candidates <- which(head_of(g, E(g))$a==0 & tail_of(g, E(g))$a==0)
# random: 20% prob for deletion
idx <- sample(c(TRUE, FALSE), length(candidates), prob = c(.2,.8), repl=T)
delete_edges(g, candidates[idx])