round_preserve_sum: Round preserving sum

View source: R/round_preserve_sum.R

round_preserve_sumR Documentation

Round preserving sum

Description

Round values preserving their sum. Often used to round percentages such that their totals still add to 100

Usage

round_preserve_sum(x, digits = 0)

Arguments

x

numeric vector to round

digits

integer indicating the number of decimal places to be used

Value

a numeric value

Examples

x <- c(10.3, 20.3, 69.4)
# These three values add to 100
sum(x)
# But when rounded they do not add to 100
sum(round(x))
# Using round_preserve_sum ensures the rounded values add to 100
round_preserve_sum(x)
sum(round_preserve_sum(x))

StatisticsNZ/er.helpers documentation built on Oct. 2, 2023, 7:24 a.m.