Bill
Bill

Reputation: 203

Igraph running an if statement on individual nodes

g <- make_ring(10) %>%
set_vertex_attr("smoke", value = 1) %>%
add_vertices(10, color = 4, "a" = 2)
g
plot(g)

if("a" == 2) {
     set_vertex_attr("a", value = 1)
}
V(g)$a

With igraph, whenever I run this code nothing happens to the the attribute values of the 10 unconnected nodes with the attribute value of 2. How would I change this code so that the if statement runs for each of these nodes and changes each one separately (dependent on the initial value of "a")?

Upvotes: 1

Views: 75

Answers (1)

G5W
G5W

Reputation: 37661

Your if statement makes no sense because

  1. There is no variable a, just an attribute of the vertices
  2. if is not a vector operator. It returns a single Boolean value

I think what you want is something like

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

Upvotes: 1

Related Questions