Converts any supported network format to an adjacency matrix.
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)
#> A B C D
#> A 0.0 0.5 0.8 0.0
#> B 0.5 0.0 0.3 0.6
#> C 0.8 0.3 0.0 0.4
#> D 0.0 0.6 0.4 0.0
# From cograph_network
net <- as_cograph(adj)
to_matrix(net)
#> A B C D
#> A 0.0 0.5 0.8 0.0
#> B 0.5 0.0 0.3 0.6
#> C 0.8 0.3 0.0 0.4
#> D 0.0 0.6 0.4 0.0
# From igraph (weighted graph)
if (requireNamespace("igraph", quietly = TRUE)) {
g <- igraph::graph_from_adjacency_matrix(adj, mode = "undirected", weighted = TRUE)
to_matrix(g)
}
#> A B C D
#> A 0.0 0.5 0.8 0.0
#> B 0.5 0.0 0.3 0.6
#> C 0.8 0.3 0.0 0.4
#> D 0.0 0.6 0.4 0.0
