R/round_preserve_sum.R
round_preserve_sum.Rd
In general, the sum of rounded numbers (e.g., using the
base::round
function) is not the same as their rounded sum.
This solution applies the following algorithm
Round down to the specified number of decimal places
Order numbers by their remainder values
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
round_preserve_sum(x, digits = 0)
https://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/ and http://stackoverflow.com/questions/32544646/round-vector-of-numerics-to-integer-while-preserving-their-sum
vector to sum
number of decimals for rounding