R 重新布线加权图生成NAs
前一个到另一个提供了一些代码来重新布线加权图,如下所示:R 重新布线加权图生成NAs,r,graph,igraph,R,Graph,Igraph,前一个到另一个提供了一些代码来重新布线加权图,如下所示: g <- graph.ring(10) E(g)$weight <- seq_len(ecount(g)) E(g)$weight # [1] 1 2 3 4 5 6 7 8 9 10 is.weighted(g) # [1] TRUE g2 <- rewire(g,niter=3) plot(g2) is.weighted(g2) # [1] TRUE 1) 手动重新分配边非常简单。但另一种简单的方
g <- graph.ring(10)
E(g)$weight <- seq_len(ecount(g))
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
is.weighted(g)
# [1] TRUE
g2 <- rewire(g,niter=3)
plot(g2)
is.weighted(g2)
# [1] TRUE
1) 手动重新分配边非常简单。但另一种简单的方法是标记边缘,然后排列标签,例如:
V(g)$name <- letters[1: ecount(g)]
E(g)
# + 10/10 edges (vertex names):
# [1] a--b b--c c--d d--e e--f f--g g--h h--i i--j a--j
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
V(g)$name <- sample(letters[1: ecount(g)])
E(g)
# + 10/10 edges (vertex names):
# [1] g--h h--c c--d d--e e--j j--a a--b b--f f--i g--i
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
# visualize:
plot(g, edge.width = E(g)$weight)
V(g)$name在我的环境中(1.0.1版)igraph
,g2谢谢@cartlefish44我刚刚也找到了这个,
g2 <- rewire(g,niter=3)
E(g2)$weight <- sample( seq_len(ecount(g)) )
id id w
A - B 6
C - D 1
E - F 1
to
id id w
A - C 6
D - E 1
B - F 1
but also does:
id id w
A - B 6
C - D 1
E - F 1
to
id id w
A - C 4
D - E 3
B - F 1
V(g)$name <- letters[1: ecount(g)]
E(g)
# + 10/10 edges (vertex names):
# [1] a--b b--c c--d d--e e--f f--g g--h h--i i--j a--j
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
V(g)$name <- sample(letters[1: ecount(g)])
E(g)
# + 10/10 edges (vertex names):
# [1] g--h h--c c--d d--e e--j j--a a--b b--f f--i g--i
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
# visualize:
plot(g, edge.width = E(g)$weight)
w <- sum(E(g)$weight)
s <- sample(w, size = ecount(g) - 1)
E(g)$weight <- c(sort(s), w) - c(0, sort(s))
sum(E(g)$weight) == w
# [1] TRUE
plot(g, edge.width = E(g)$weight)