scale_rounded_by_sum: Scale values so that the rounded values sum to 1

View source: R/sequence_characteristics.R

scale_rounded_by_sumR Documentation

Scale values so that the rounded values sum to 1

Description

Scale values so that the rounded values sum to 1

Usage

scale_rounded_by_sum(
  x,
  digits,
  icolumn_adjust = 1L,
  max_iter = max(4L, digits + 1L),
  verbose = FALSE
)

Arguments

x

A one or two-dimensional numeric object with positive values: a vector, data frame, matrix, or array. A vector is treated as a 1-row matrix.

digits

A integer value. The number of decimal places to round; a number between one and four.

icolumn_adjust

An integer value. The column number of x that will be adjusted if rounded values have not converged to sum to 1 after max_iter.

max_iter

An integer value. The maximum number of iterations.

verbose

A logical value. Reports a warning if TRUE and max_iter is exceeded.

Notes

This function may fail due to integer division by zero.

This function may also not converge to round values so that they sum to 1, particularly if too few digits are available to round. In that case, attempt to increase digits. However, the return values should sum to within digits of 1.

See Also

scale_by_sum, round

Examples

x <- c(1:6, 8) / 10
scale_rounded_by_sum(x, digits = 1, icolumn_adjust = 7)
scale_rounded_by_sum(x, digits = 2, icolumn_adjust = 7)
scale_rounded_by_sum(x, digits = 4, icolumn_adjust = 7)

x <- matrix(1:12, nrow = 4, ncol = 3)
scale_rounded_by_sum(x, digits = 1)
scale_rounded_by_sum(x, digits = 2)
scale_rounded_by_sum(x, digits = 4)

x <- as.data.frame(x)
scale_rounded_by_sum(x, digits = 1)


DrylandEcology/rSW2utils documentation built on Dec. 9, 2023, 10:44 p.m.