繁体   English   中英

如何基于R中igraph中的节点属性值在节点之间生成边缘?

[英]How do I generate an edge between nodes based on the node attribute values in igraph in R?

如果我有如下代码:

g <- erdos.renyi.game(20, 50 , type = "gnm" , directed = F , loops = F) %>%
  set_vertex_attr("a", value = 0) 

total <- vcount(g)

V(g)$a <- sample(rep(0:2, c(1, total -5, 4)), total)

如何生成将属性“ a”值为0的一个节点连接到属性“ a”值为1的随机节点的随机边?

试试这个来自igraph add_edges

g <- erdos.renyi.game(20, 50 , type = "gnm" , directed = F , loops = F) %>% set_vertex_attr("a", value = 0) 
total <- vcount(g)
V(g)$a <- c(2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 0L, 1L, 1L, 1L, 1L, 2L, 1L, 0L)
# generate a random edge that connects the one node with an attribute "a" value of 0 to a random node that has an attribute "a" value of 1
g <- add_edges(g, c(sample(V(g)[V(g)$a == 0], 1), sample(V(g)[V(g)$a == 1], 1)))

暂无
暂无

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

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