Skip to contents

Converts an input list of networks between various representations: matrix, dgCMatrix, edgelist, and igraph.

Usage

make_network_type(obj, input.type, output.type)

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".

Value

A converted list of network objects in the specified output.type format.

Details

The function converts each network in obj to the specified output.type. Conversion logic is applied based on input.type and output.type.

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.

See also

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"