R/matrix_to_edge_list.R
matrix_to_edge_list.Rd
Converts a connectivity matrix to an edge list. This function allows to
create the same edge list as the one obtained with create_edge_list()
.
matrix_to_edge_list(x, all = FALSE)
a matrix
object. The connectivity matrix to be converted in an
edge list.
a logical
value. If FALSE
(default), removes missing edges.
A data.frame
with two (or three) columns:
from
: label of one of the two nodes of the edge
to
: label of the other node of the edge
edge
: 0 (no edge) or 1 (edge). This column is returned only if
all = TRUE
.
library("chessboard")
# Two-dimensional sampling ----
sites_infos <- expand.grid("transect" = 1:3, "quadrat" = 1:5)
sites_infos
#> transect quadrat
#> 1 1 1
#> 2 2 1
#> 3 3 1
#> 4 1 2
#> 5 2 2
#> 6 3 2
#> 7 1 3
#> 8 2 3
#> 9 3 3
#> 10 1 4
#> 11 2 4
#> 12 3 4
#> 13 1 5
#> 14 2 5
#> 15 3 5
nodes <- create_node_labels(data = sites_infos,
transect = "transect",
quadrat = "quadrat")
edges <- create_edge_list(nodes, method = "pawn", directed = TRUE)
conn_matrix <- connectivity_matrix(edges)
# Convert back to edge list ----
new_edges <- matrix_to_edge_list(conn_matrix)
new_edges
#> from to edge
#> 1 1-1 1-2 1
#> 2 1-2 1-3 1
#> 3 1-3 1-4 1
#> 4 1-4 1-5 1
#> 5 2-1 2-2 1
#> 6 2-2 2-3 1
#> 7 2-3 2-4 1
#> 8 2-4 2-5 1
#> 9 3-1 3-2 1
#> 10 3-2 3-3 1
#> 11 3-3 3-4 1
#> 12 3-4 3-5 1
# Check ----
identical(edges, new_edges)
#> [1] FALSE