roundSum: Round elements in matrix or data.frame rows

Description Usage Arguments Details Value Examples

View source: R/approxExtrapDemand.R

Description

Round all numbers in a matrix or data.frame while ensuring that all rows sum to the same value.

Usage

1
roundSum(x, n, digits = 0, ...)

Arguments

x

matrix or data.frame

n

numeric specifying the target sum for each row in x

digits

integer indicating the number of decimal places to be used

...

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## Not run: 

## Sibuyan Island

## load observed land use data and create demand scenario
obs <- LulcRasterStack(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)


## End(Not run)

simonmoulds/lulcc2 documentation built on Dec. 23, 2021, 2:24 a.m.