O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a graph object with weights

If the name of the third column in the edge list passed to graph.data.frame is called weight, it creates a weighted graph. Hence, we can rename the third column from x to weight and redraw the graph:

> nw.weights <- nw.new 
> names(nw.weights) <- c("i","j","weight") 
> g.weights <- graph.data.frame(nw.weights, directed=FALSE) 
> g.weights 
IGRAPH UNW- 18 19 --  
+ attr: name (v/c), weight (e/n)  

When we check the graph properties, we see W in the third position of UNW-.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required