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

Deleting isolates from a graph

Isolated nodes have no connections or edges and therefore have degree 0. We can use this to select vertices that have 0 degree and delete them using delete.vertices as follows:

> g.new <- delete.vertices(g.new, V(g.new)[ degree(g.new)==0 ]) 

We can also use delete.vertices to delete a specific vertex. This function creates a new graph. If the vertex does not exist in the graph, you will see an error message Invalid vertex names. Plot the new graph to check whether the isolated vertex has been removed:

> g.new <- delete.vertices(g.new,12) 

Deletion reassigns the vertex IDs even in some cases when edges are deleted. Hence, if you are using IDs instead of vertex names, exercise caution as the IDs may change.

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