Convert a data.frame object that follows the Marxan format to a matrix format. This function is useful for converting data.frame objects to matrix or array objects that are used by the various penalties and constraints functions. If the boundary data contains data for a single zone, then a matrix object is returned. Otherwise if the boundary data contains data for multiple zones, then an array is returned.

marxan_boundary_data_to_matrix(x, data)

Arguments

x

ConservationProblem-class 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.

Value

array or sparse matrix (dgCMatrix-class) object.

Examples

# create marxan boundary with four planning units and one zone bldf1 <- expand.grid(id1 = seq_len(4), id2 = seq_len(4)) bldf1$boundary <- 1 bldf1$boundary[bldf1$id1 == bldf1$id2] <- 0.5 # convert to matrix m1 <- marxan_boundary_data_to_matrix(NULL, bldf1) # visualize matrix image(m1)
# create marxan boundary with three planning units and two zones bldf2 <- expand.grid(id1 = seq_len(3), id2 = seq_len(3), zone1 = c("z1", "z2"), zone2 = c("z1", "z2")) bldf2$boundary <- 1 bldf2$boundary[bldf2$id1 == bldf2$id2 & bldf2$zone1 == bldf2$zone2] <- 0.5 bldf2$boundary[bldf2$id1 == bldf2$id2 & bldf2$zone1 != bldf2$zone2] <- 0 # convert to array m2 <- marxan_boundary_data_to_matrix(NULL, bldf2) # print array print(m2)
#> , , 1, 1 #> #> [,1] [,2] [,3] #> [1,] 0.5 1.0 1.0 #> [2,] 1.0 0.5 1.0 #> [3,] 1.0 1.0 0.5 #> #> , , 2, 1 #> #> [,1] [,2] [,3] #> [1,] 0 1 1 #> [2,] 1 0 1 #> [3,] 1 1 0 #> #> , , 1, 2 #> #> [,1] [,2] [,3] #> [1,] 0 1 1 #> [2,] 1 0 1 #> [3,] 1 1 0 #> #> , , 2, 2 #> #> [,1] [,2] [,3] #> [1,] 0.5 1.0 1.0 #> [2,] 1.0 0.5 1.0 #> [3,] 1.0 1.0 0.5 #>