user8267234
user8267234

Reputation:

iGraph R package bug?

When executing the simple code below to add weight to an edge from node 3 to node 5 in the network and looking edge weights it shows NA values there.

g<-make_empty_graph(directed = F)

g<-add.vertices(g,c(10))

g<-add_edges(g,c(3,5))
g<-set_edge_attr(graph = g,name="weight",index = c(3,5),value = 0.3)

E(g)$weight
plot(g)

After execution I get this

> E(g)$weight
[1] NA
> plot(g)
> 

Is this a bug or I'm doing something incorrectly?

Upvotes: 2

Views: 78

Answers (1)

LocoGris
LocoGris

Reputation: 4480

The issue is that you have specified badly the index argument when using set_edge_attr:

This will produce the correct result:

g<-make_empty_graph(directed = F)

g<-add.vertices(g,c(10))

g<-add_edges(g,c(3,5))
g<-set_edge_attr(graph = g,name="weight",index = E(g),value = 0.3)

E(g)$weight
plot(g)

As you can see from ?set_edge_attr:

index: An optional edge sequence to set the attributes of a subset of edges

So now, let say you have another edge and want to set it a value of 10:

g<-make_empty_graph(directed = F)

g<-add.vertices(g,c(10))

g<-add_edges(g,c(3,5))
g<-add_edges(g,c(4,5))
g<-set_edge_attr(graph = g,name="weight",index = E(g)[1],value = 0.3)
g<-set_edge_attr(graph = g,name="weight",index = E(g)[2],value = 10)

E(g)$weight
plot(g)

You use E(g)[1] for the first and E(g)[2] because E(g) gives you back an array of all your edges in the order you specified them (1 will be c(3,5) and 2 will be c(4,5))

Best!

Upvotes: 1

Related Questions