roundSum {lulcc} | R Documentation |
Round elements in matrix or data.frame rows
Description
Round all numbers in a matrix or data.frame while ensuring that all rows sum to the same value.
Usage
roundSum(x, ncell, ...)
Arguments
x |
matrix or data.frame |
ncell |
numeric specifying the target sum for each row in |
... |
additional arguments (none) |
Details
The main application of roundSum
is to ensure that each row in the
demand matrix specifies exactly the number of cells to be allocated to each
land use category for the respective timestep. It may also be used to convert
the units of demand to number of cells.
Value
A matrix.
Examples
## Sibuyan Island
## load observed land use data and create demand scenario
obs <- ObsLulcRasterStack(x=sibuyan$maps,
pattern="lu",
categories=c(1,2,3,4,5),
labels=c("Forest","Coconut","Grass","Rice","Other"),
t=c(0,14))
dmd <- approxExtrapDemand(obs, tout=0:14)
apply(dmd, 1, sum)
## artificially perturb for illustration purposes
dmd <- dmd * runif(1)
apply(dmd, 1, sum)
## use roundSum to correct demand scenario
ncell <- length(which(!is.na(getValues(sibuyan$maps$lu_sib_1997))))
ncell
dmd <- roundSum(dmd, ncell=ncell)
apply(dmd, 1, sum)
[Package lulcc version 1.0.4 Index]