round_preserve_sum: Round + Preserve the Sum

Description Usage Arguments References Examples

View source: R/round_preserve_sum.R

Description

  1. Round down to the specified number of decimal places

  2. Order numbers by their remainder values

  3. Increment the specified decimal place of values with ‘k’ largest remainders, where ‘k’ is the number of values that must be incremented to preserve their rounded sum

Usage

1
round_preserve_sum(x, digits = 4)

Arguments

x

A numeric vector to apply the algorithm to. This should be a proportion vector where all values equal 1.

digits

A numeric. Indicates the number of decimal places to be used.

References

https://www.r-bloggers.com/2016/07/round-values-while-preserve-their-rounded-sum-in-r/

Examples

1
2
3
4
5
sum(c(0.333, 0.333, 0.334))

sum(round(c(0.333, 0.333, 0.334), 2))

sum(round_preserve_sum(c(0.333, 0.333, 0.334), 2))

KoderKow/kowr documentation built on July 19, 2021, 4:18 p.m.