Converts an input list of networks between various representations: matrix
, dgCMatrix
, edgelist
, and igraph
.
Arguments
- obj
A list of network objects, which must match the
input.type
specified.- input.type
A character string specifying the input type of
obj
. Options include"igraph"
,"matrix"
,"dgCMatrix"
, or"edgelist"
.- output.type
A character string specifying the desired output type. Options are
"igraph"
,"matrix"
,"dgCMatrix"
, or"edgelist"
.
Details
The function converts each network in obj
to the specified output.type
. Conversion logic is applied based on input.type
and output.type
.
If
input.type
andoutput.type
are the same,obj
is returned as is.Conversions from adjacency matrices to igraph objects are handled via
igraph::graph_from_adjacency_matrix
.Conversions from igraph objects to edge lists are handled with
igraph::as_edgelist
.Conversions from igraph objects to adjacency matrices are handled with
igraph::as_adjacency_matrix
.
Networks with \(n\) nodes can be represented as,
"igraph": an igraph object
"matrix": an \(n\)-by-\(n\) adjacency matrix, class
"matrix"
"dgCMatrix": an \(n\)-by-\(n\) adjacency matrix as a sparse matrix, class
{dgCMatrix}
, see Matrix package for details"edgelist": an \(\binom{n}{2}\)-by-2 matrix where each row is the pair of nodes an edge is attached to
To use "dgCMatrix"
for input.type
or output.type
, the package Matrix
must be installed.
Examples
# Convert list of adjacency matrices to igraph objects
one_matrix <- matrix(c(0, 1, 1, 1, 0, 0, 1, 0, 0), nrow = 3, byrow = TRUE)
matrix_list <- replicate(3, one_matrix, simplify = FALSE)
igraph_list <-
make_network_type(matrix_list,
input.type = "matrix",
output.type = "igraph")
# Same thing now using detect type
type <- detect_type(matrix_list)
type
#> [1] "matrix"
igraph_list <-
make_network_type(matrix_list,
input.type = type,
output.type = "igraph")
# Convert list of igraph objects to sparse matrix
sparsematrix_list <-
make_network_type(igraph_list,
input.type = "igraph",
output.type = "dgCMatrix")
detect_type(sparsematrix_list)
#> [1] "dgCMatrix"