Skip to contents

Converting between graph classes

As mentioned above, a common network analysis workflow includes the conversion of graph objects between various formats. The igraph and statnet packages provide some very basic functions for this, that are specific to the types of objects that are used in these packages themselves:

Graph object conversion
Convert to an adjacency matrix
igraph
igraph::as_adjacency_matrix(g, sparse = FALSE)
network
network::as.sociomatrix(g)
network::as.matrix.network(g)
Convert to an adjacency list
igraph
igraph::as_adj_list(g)
network
Convert to an edge list
igraph
igraph::as_edgelist(g)
igraph::as_data_frame(g)
network
network::as.data.frame.network(g)
network::as.edgelist(g)

# if you want to include edge weights
sna::as.edgelist.sna(g)
Make the network directed
snafun
igraph
igraph::as.directed(g)
network
Make the network undirected
snafun
# from an adjacency matrix as input
snafun::to_symmetric_matrix(g,
  rule = c("weak", "mutual", "out", "in", "average", "max", "min"),
  na.rm = TRUE)
igraph
igraph::as.undirected(g)

# for example
igraph::as.undirected(g, mode = 'collapse', edge.attrib.comb = list(weight = 'sum'))
network
# from an adjacency matrix as input
sna::symmetrize(g)

# for example
sna::symmetrize(g, rule = "strong")
Project a bipartite graph
snafun
igraph
igraph::bipartite_projection(g)
network
Convert to a line graph
snafun
igraph
igraph::make_line_graph(g)
network

Conversion between various formats is a lot easier with the help of the snafun package. You only need to know some very easy function names to convert between the common graph classes. Here is an overview of which function to use for which conversion:

Convert between various formats
Using the consistent functions of snafun
INPUT OUTPUT
edgelist matrix igraph network
edgelist to_edgelist() to_matrix() to_igraph() to_network()
matrix to_edgelist() to_matrix() to_igraph() to_network()
igraph to_edgelist() to_matrix() to_igraph() to_network()
network to_edgelist() to_matrix() to_igraph() to_network()
Conversion includes bipartite graphs.

Check the arguments for the various options.