Convert a data.frame object containing Marxan connectivity data to matrix format. This function is designed specifically for connectivity data (not boundary data). It ensures that the output matrix correctly specifies symmetric or asymmetric connectivity relationships between planning units.

marxan_connectivity_data_to_matrix(x, data, symmetric = TRUE)

Arguments

x

problem() (i.e., ConservationProblem) object that contains planning unit and zone data to ensure that the argument to data is converted correctly. This argument can be set to NULL if checks are not required (not recommended).

data

data.frame object with the columns "id1", "id2", and "boundary". The columns "zone1" and "zone2" can also be provided to indicate zone data.

symmetric

logical does the connectivity data describe symmetric relationships between planning units? If the data contain asymmetric connectivity data, this parameter should be set to FALSE. Defaults to TRUE.

Value

array or dgCMatrix sparse matrix object. If the argument to data corresponds to a single zone, then a dgCMatrix

matrix is returned. Otherwise, if the argument to data

corresponds to multiple zones, then an array is returned.

Examples

# create marxan connectivity data with four planning units and one zone,
# and symmetric connectivity values
bldf1 <- expand.grid(id1 = seq_len(4), id2 = seq_len(4))
bldf1$boundary <- 1
bldf1$boundary[bldf1$id1 == bldf1$id2] <- 0.5

# print data
print(bldf1)
#>    id1 id2 boundary
#> 1    1   1      0.5
#> 2    2   1      1.0
#> 3    3   1      1.0
#> 4    4   1      1.0
#> 5    1   2      1.0
#> 6    2   2      0.5
#> 7    3   2      1.0
#> 8    4   2      1.0
#> 9    1   3      1.0
#> 10   2   3      1.0
#> 11   3   3      0.5
#> 12   4   3      1.0
#> 13   1   4      1.0
#> 14   2   4      1.0
#> 15   3   4      1.0
#> 16   4   4      0.5

# convert to matrix
m1 <- marxan_connectivity_data_to_matrix(NULL, bldf1)

# print matrix
print(m1)
#> 4 x 4 sparse Matrix of class "dsCMatrix"
#>                     
#> [1,] 0.5 1.0 1.0 1.0
#> [2,] 1.0 0.5 1.0 1.0
#> [3,] 1.0 1.0 0.5 1.0
#> [4,] 1.0 1.0 1.0 0.5

# visualize matrix
# \dontrun{
image(m1)

# }
# create marxan connectivity data with four planning units and one zone,
# and asymmetric connectivity values
bldf2 <- expand.grid(id1 = seq_len(4), id2 = seq_len(4))
bldf2$boundary <- runif(nrow(bldf2))
bldf2$boundary[bldf1$id1 == bldf1$id2] <- 0.5

# print data
print(bldf2)
#>    id1 id2   boundary
#> 1    1   1 0.50000000
#> 2    2   1 0.48469318
#> 3    3   1 0.85434472
#> 4    4   1 0.58924564
#> 5    1   2 0.57586891
#> 6    2   2 0.50000000
#> 7    3   2 0.23870311
#> 8    4   2 0.23721283
#> 9    1   3 0.23125087
#> 10   2   3 0.98898790
#> 11   3   3 0.50000000
#> 12   4   3 0.06918062
#> 13   1   4 0.19277122
#> 14   2   4 0.65083831
#> 15   3   4 0.07945488
#> 16   4   4 0.50000000

# convert to matrix
m2 <- marxan_connectivity_data_to_matrix(NULL, bldf2, symmetric = FALSE)

# print matrix
print(m2)
#> 4 x 4 sparse Matrix of class "dgCMatrix"
#>                                               
#> [1,] 0.5000000 0.5758689 0.23125087 0.19277122
#> [2,] 0.4846932 0.5000000 0.98898790 0.65083831
#> [3,] 0.8543447 0.2387031 0.50000000 0.07945488
#> [4,] 0.5892456 0.2372128 0.06918062 0.50000000

# visualize matrix
# \dontrun{
image(m2)

# }
# create marxan connectivity with three planning units and two zones,
# and symmetric connectivity values
bldf3 <- expand.grid(id1 = seq_len(3), id2 = seq_len(3),
                     zone1 = c("z1", "z2"),
                     zone2 = c("z1", "z2"))
bldf3$boundary <- 1
bldf3$boundary[bldf2$id1 == bldf2$id2 & bldf2$zone1 == bldf2$zone2] <- 0.5
bldf3$boundary[bldf2$id1 == bldf2$id2 & bldf2$zone1 != bldf2$zone2] <- 0

# print data
print(bldf3)
#>    id1 id2 zone1 zone2 boundary
#> 1    1   1    z1    z1        1
#> 2    2   1    z1    z1        1
#> 3    3   1    z1    z1        1
#> 4    1   2    z1    z1        1
#> 5    2   2    z1    z1        1
#> 6    3   2    z1    z1        1
#> 7    1   3    z1    z1        1
#> 8    2   3    z1    z1        1
#> 9    3   3    z1    z1        1
#> 10   1   1    z2    z1        1
#> 11   2   1    z2    z1        1
#> 12   3   1    z2    z1        1
#> 13   1   2    z2    z1        1
#> 14   2   2    z2    z1        1
#> 15   3   2    z2    z1        1
#> 16   1   3    z2    z1        1
#> 17   2   3    z2    z1        1
#> 18   3   3    z2    z1        1
#> 19   1   1    z1    z2        1
#> 20   2   1    z1    z2        1
#> 21   3   1    z1    z2        1
#> 22   1   2    z1    z2        1
#> 23   2   2    z1    z2        1
#> 24   3   2    z1    z2        1
#> 25   1   3    z1    z2        1
#> 26   2   3    z1    z2        1
#> 27   3   3    z1    z2        1
#> 28   1   1    z2    z2        1
#> 29   2   1    z2    z2        1
#> 30   3   1    z2    z2        1
#> 31   1   2    z2    z2        1
#> 32   2   2    z2    z2        1
#> 33   3   2    z2    z2        1
#> 34   1   3    z2    z2        1
#> 35   2   3    z2    z2        1
#> 36   3   3    z2    z2        1

# convert to array
m3 <- marxan_connectivity_data_to_matrix(NULL, bldf3)

# print array
print(m3)
#> , , 1, 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    1    1
#> [3,]    1    1    1
#> 
#> , , 2, 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    1    1
#> [3,]    1    1    1
#> 
#> , , 1, 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    1    1
#> [3,]    1    1    1
#> 
#> , , 2, 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    1    1
#> [3,]    1    1    1
#>