Skip to contents

Manipulating the graph object

Once you have a network object, you will want to explore it and access parts of it or extract info from it.

Here is a table with some of the functions you’ll use all the time in any SNA project.

Manipulate the graph object
Print the graph object
snafun
snafun::print(x)
igraph1
igraph::print.igraph(x)
network1
network::print.network(x)
Check characteristics of the graph object
snafun
snafun::has_edge_attributes(x)

snafun::has_vertex_attributes(x)

snafun::has_vertex_attribute(x, attrname)

snafun::has_edge_attribute(x, attrname)

snafun::has_vertexnames(x)

snafun::has_loops(x)

snafun::is_bipartite(x)

snafun::is_connected(x, rule = c("weak", "strong"))

snafun::is_directed(x)

snafun::is_network(x)

snafun::is_igraph(x)

snafun::is_signed(x)

snafun::is_weighted(x)
igraph
# various functions, including
igraph::any_loop(g)
igraph::any_multiple(g)
igraph::is_bipartite(graph)
igraph::is_connected(graph, mode = c("weak", "strong"))
igraph::is_directed(graph)
igraph::is_igraph(graph)
igraph::is_named(graph)
igraph::is_simple(graph)
igraph::is_weighted(graph)
network
# various functions, including:
network::has.loops(x)
network::is.bipartite(x)
network::is.directed(x)
network::is.multiplex(x)
network::is.network(x)
sna::is.connected(g, connected = "strong")
Access the vertices and edges
snafun
igraph
igraph::V(x)              # vertices
igraph::E(x)              # edges
network
Extract vertex / edge / graph attributes
snafun
snafun::extract_vertex_attribute(x, name)
snafun::extract_vertex_names(x)             # specific to access the vertex names attribute
snafun::extract_edge_attribute(x, name)
snafun::extract_graph_attribute(x, name)
igraph
igraph::vertex_attr(graph, name, index = V(graph))
igraph::V(graph)
attributenameigraph::edgeattr(graph,name,index=E(graph))igraph::E(graph)attributename
    
    igraph::edge_attr(graph, name, index = E(graph))
    igraph::E(graph)attributename

igraph::graph_attr(graph, name)
network2
network::get.vertex.attribute(x, attrname)
network::get.edge.attribute(x, attrname)
network::get.network.attribute(x, attrname)
igraph
network
Extract the ID of an edge
snafun
snafun::extract_edge_id(object, ego, alter, edgelist)
Extract vertex names
snafun
snafun::extract_vertex_names(x)
igraph
igraph::V(x)$name
igraph::vertex_attr(x, name = "names")
network
network::get.vertex.attribute(x, "vertex.names")
network::network.vertex.names(x)
set vertex names
snafun
snafun::add_vertex_names(x, value)
igraph
igraph::V(x)$name <- value
igraph::set_vertex_attr(x, name = "names")
network
network::set.vertex.attr(x, "vertex.names", value)
network::network.vertex.names(x) <- value
List vertex / edge / graph attributes
snafun
snafun::list_vertex_attribute(x, name)
snafun::list_edge_attribute(x, name)
snafun::list_graph_attribute(x, name)
igraph
igraph::vertex_attr_names(graph)
igraph::edge_attr_names(graph)
igraph::graph_attr_names(graph)
network
network::list.vertex.attributes(x)
network::list.edge.attributes(x
network::list.network.attributes(x)
Extract all vertex attributes into a data.frame
snafun
snafun::extract_all_vertex_attributes(g)
igraph
network
Add vertex / edge / graph attributes
snafun
# very flexible and powerful functions to add attributes 
# in several ways, with the same function
snafun::add_edge_attributes(object, attr_name, value, edgelist, overwrite = FALSE)

snafun::add_vertex_attributes(x, attr_name = NULL, value)
snafun::add_graph_attribute(x, attr_name = NULL, value)
igraph
igraph::set_edge_attr(g, "name", value)
igraph::E(g)
name<valueigraph::setvertexattr(g,"name",value)igraph::V(g)name <- value
    
    igraph::set_vertex_attr(g, "name", value)
    igraph::V(g)name <- value

igraph::set_graph_attr(g, "name", value)
g$name <- value
network
network::set.edge.attribute(g, "new_name", value, e = seq_along(g
mel))network::set.edge.value(g,"newname",value,e=seqalong(gmel))
    network::set.edge.value(g, "new_name", value, e = seq_along(gmel))

network::set.network.attribute(g, "new_name", value)

network::set.vertex.attribute(g, "new_name", value, v = seq_len(network::network.size(g)))
Remove vertex / edge / graph attributes
snafun
snafun::remove_edge_attribute(x, attr_name)
snafun::remove_edge_weight(x)  # remove edge weights

snafun::remove_vertex_attribute(x, attr_name)
snafun::remove_vertex_names(x)   # specific for the vertex name attribute

snafun::remove_graph_attribute(x, attr_name)
igraph
igraph::delete_edge_attr(graph, name)
igraph::delete_vertex_attr(graph, name)
igraph::delete_graph_attr(graph, name)
network
network::delete.edge.attribute(x, attrname, ...)
network::delete.vertex.attribute(x, attrname, ...)
network::delete.network.attribute(x, attrname, ...)
Add vertices or edges
snafun
igraph
igraph::add_vertices(graph, nv, ..., attr = list())
igraph::add_edges(graph, edges, ..., attr = list())
network
network::add.vertices(x, nv, vattr = NULL, last.mode = TRUE, ...)
network::add.edge(x, tail, head, names.eval = NULL, vals.eval = NULL, edge.check = FALSE, ...)
network::add.edges(x, tail, head, names.eval = NULL, vals.eval = NULL, ...)
Remove vertices or edges
snafun
snafun::remove_vertices(x, vertices)
igraph
igraph::delete_vertices(graph, v)
igraph::delete_edges(graph, edges)
network
network::delete.edges(x, eid)
network::delete.vertices(x, vid)
Contract vertices into one
snafun
snafun::contract_vertices(g, vertices, method = c("min", "max", "union", "add"),
  attr = NULL, new_name = "set", out_as_igraph = TRUE)
igraph
network
Extract a subset from the graph
snafun
# single function to do it all
snafun::extract_subgraph(x, v_to_keep, e_to_keep)
igraph
# subset based on vertices
igraph::induced_subgraph(g, vids = theVerticesYouWantToKeep)

# subset based on edges
igraph::subgraph.edges(g, eids = theEdgesYouWantToKeep)
network
# subset based on vertices
network::get.inducedSubgraph(g, v = theVerticesYouWantToKeep)

# subset based on edges
network::get.inducedSubgraph(g, eid = theEdgesYouWantToKeep)
Extract egonets from the graph
snafun
snafun::extract_egonet(x, vertices = NULL, order = 1, type = c("all", "out", "in"))
igraph
igraph::make_ego_graph(g, order = 1, nodes = V(graph), mode = c("all", "out", "in"), mindist = 0)
network
sna::ego.extract(dat, ego = NULL, neighborhood = c("combined", "in", "out"))
Clean up the graph
snafun
snafun::find_isolates(x, names = TRUE, loops = FALSE)
snafun::remove_isolates(x, loops = FALSE)
snafun::remove_loops(x)
igraph
igraph::simplify(g)

# for example
igraph::simplify(g, remove.multiple = TRUE,
    remove.loops = TRUE, edge.attrib.comb = list(weight = 'max'))
network
sna::isolates(g, diag=FALSE)
Union of graphs
snafun
snafun::make_union(net1, net2, net3, byname = 'auto')
igraph
igraph::union(net1, net2, net3)
network
Intersection of graphs
snafun
snafun::extract_intersection(net, net2, net3, byname = 'auto', keep.all.vertices = TRUE)
igraph
igraph::intersection(net, net2, net3)
network
1 Often, but certainly not always, it will also work if you just use `print(x)`
2 These functions have many additional arguments