to_matrix: Convert Network to Adjacency Matrix

View source: R/network-utils.R

to_matrixR Documentation

Convert Network to Adjacency Matrix

Description

Converts any supported network format to an adjacency matrix.

Usage

to_matrix(x, directed = NULL)

Arguments

x

Network input: matrix, cograph_network, igraph, network, tna, etc.

directed

Logical or NULL. If NULL (default), auto-detect from input.

Value

A square numeric adjacency matrix with row/column names.

See Also

to_igraph, to_df, as_cograph, to_network

Examples

# From matrix
adj <- matrix(c(0, .5, .8, 0,
                .5, 0, .3, .6,
                .8, .3, 0, .4,
                 0, .6, .4, 0), 4, 4, byrow = TRUE)
rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D")
to_matrix(adj)

# From cograph_network
net <- as_cograph(adj)
to_matrix(net)

# From igraph (weighted graph)
if (requireNamespace("igraph", quietly = TRUE)) {
  g <- igraph::graph_from_adjacency_matrix(adj, mode = "undirected", weighted = TRUE)
  to_matrix(g)
}

cograph documentation built on April 1, 2026, 1:07 a.m.