igraph为属性值替换添加多个先决条件

时间:2017-11-13 01:29:55

标签: r igraph

所以我有一个图表和代码如下:

g <- make_ring(10) %>%
set_vertex_attr("a", value = 3) %>%
set_vertex_attr("b", value = 2) %>%
add_vertices(10, color = 4, "a" = 2, "b" = 3) %>%
add_vertices(10, color = 4, "a" = 2, "b" = 4)
g

V(g)$a[V(g)$a == 2] = 1
V(g)$a

我想改变

V(g)$a[V(g)$a == 2] = 1 

以便在&#34; a&#34;之前必须满足更多先决条件。更改为1

我试过

V(g)$a[V(g)$a == 2,V(g)$b == 4] = 1 

但它返回了: V(g)中的错误$ a [V(g)$ a == 2,V(g)$ b == 4] = 1:   矩阵上的下标数量不正确。我该如何重写这段代码?

1 个答案:

答案 0 :(得分:1)

我认为你只想要

V(g)$a[V(g)$a == 2 & V(g)$b == 4] = 1 
V(g)$a
[1] 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1