Converts an edge list to an connectivity matrix (also known as adjacency matrix).

connectivity_matrix(
  edges,
  lower = TRUE,
  upper = TRUE,
  diag = TRUE,
  na_to_zero = TRUE
)

Arguments

edges

a data.frame with the following two columns: from (the first node of the edge) and to (the second node of the edge). The output of the functions create_edge_list() or append_edge_lists().

lower

a logical value. If TRUE (default), keep values in the lower triangle of the matrix. Otherwise they will be replaced by NA (or 0).

upper

a logical value. If TRUE (default), keep values in the upper triangle of the matrix. Otherwise they will be replaced by NA (or 0).

diag

a logical value. If TRUE (default), keep values in the diagonal of the matrix. Otherwise they will be replaced by NA (or 0).

na_to_zero

a logical value. If TRUE (default), missing edges are coded as 0. Otherwise they will be coded as NA.

Value

A connectivity matrix of dimensions n x n, where n is the number of nodes.

Examples

# Import Adour sites ----
path_to_file <- system.file("extdata", "adour_survey_sampling.csv", 
                            package = "chessboard")
adour_sites  <- read.csv(path_to_file)

# Select first location ----
adour_sites <- adour_sites[adour_sites$"location" == 1, ]

# Create node labels ----
adour_nodes <- create_node_labels(data     = adour_sites, 
                                  location = "location", 
                                  transect = "transect", 
                                  quadrat = "quadrat")

# Find edges with 1 degree of neighborhood (pawn method) ----
adour_edges <- create_edge_list(adour_nodes, method = "pawn", 
                                directed = TRUE)

# Get connectivity matrix ----
connectivity_matrix(adour_edges)
#>     1-1 1-2 1-3 1-4 1-5 2-1 2-2 2-3 2-4 2-5 3-1 3-2 3-3 3-4 3-5
#> 1-1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
#> 1-2   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0
#> 1-3   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0
#> 1-4   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
#> 1-5   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> 2-1   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0
#> 2-2   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0
#> 2-3   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0
#> 2-4   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0
#> 2-5   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> 3-1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0
#> 3-2   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0
#> 3-3   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0
#> 3-4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1
#> 3-5   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

# Get connectivity matrix ----
connectivity_matrix(adour_edges, na_to_zero = FALSE)
#>     1-1 1-2 1-3 1-4 1-5 2-1 2-2 2-3 2-4 2-5 3-1 3-2 3-3 3-4 3-5
#> 1-1  NA   1  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 1-2  NA  NA   1  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 1-3  NA  NA  NA   1  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 1-4  NA  NA  NA  NA   1  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 1-5  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2-1  NA  NA  NA  NA  NA  NA   1  NA  NA  NA  NA  NA  NA  NA  NA
#> 2-2  NA  NA  NA  NA  NA  NA  NA   1  NA  NA  NA  NA  NA  NA  NA
#> 2-3  NA  NA  NA  NA  NA  NA  NA  NA   1  NA  NA  NA  NA  NA  NA
#> 2-4  NA  NA  NA  NA  NA  NA  NA  NA  NA   1  NA  NA  NA  NA  NA
#> 2-5  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 3-1  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA   1  NA  NA  NA
#> 3-2  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA   1  NA  NA
#> 3-3  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA   1  NA
#> 3-4  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA   1
#> 3-5  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA