Round values preserving their sum. Often used to round percentages such that their totals still add to 100
round_preserve_sum(x, digits = 0)a numeric value
x <- c(10.3, 20.3, 69.4)
# These three values add to 100
sum(x)
#> [1] 100
# But when rounded they do not add to 100
sum(round(x))
#> Error in round(x): argument "digits" is missing, with no default
# Using round_preserve_sum ensures the rounded values add to 100
round_preserve_sum(x)
#> [1] 10 20 70
sum(round_preserve_sum(x))
#> [1] 100