specificNumPerPatch {SpaDES.tools} | R Documentation |
Initiate a specific number of agents in a map of patches
Description
Instantiate a specific number of agents per patch.
The user can either supply a table of how many to initiate in each patch,
linked by a column in that table called pops
.
Usage
specificNumPerPatch(patches, numPerPatchTable = NULL, numPerPatchMap = NULL)
Arguments
patches |
|
numPerPatchTable |
A |
numPerPatchMap |
A |
Value
A raster with 0s and 1s, where the 1s indicate starting locations of agents following the numbers above.
Examples
library(data.table)
origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)
set.seed(1234)
Ntypes <- 4
ras <- randomPolygons(numTypes = Ntypes)
if (interactive()) {
terra::plot(ras)
}
# Use numPerPatchTable
patchDT <- data.table(pops = 1:Ntypes, num.in.pop = c(1, 3, 5, 7))
rasAgents <- specificNumPerPatch(ras, patchDT)
rasAgents[is.na(rasAgents)] <- 0
if (require(testthat))
expect_true(all(unname(table(ras[rasAgents])) == patchDT$num.in.pop))
# Use numPerPatchMap
rasPatches <- ras
for (i in 1:Ntypes) {
rasPatches[rasPatches==i] <- patchDT$num.in.pop[i]
}
if (interactive()) {
terra::plot(c(ras, rasPatches))
}
rasAgents <- specificNumPerPatch(ras, numPerPatchMap = rasPatches)
rasAgents[is.na(rasAgents)] <- 0
if (interactive()) {
terra::plot(rasAgents)
}
# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)
[Package SpaDES.tools version 2.0.7 Index]