簡體   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