rounded_sum: Round a vector of numbers such that their sum stays.

Description Usage Arguments Details Value

Description

Applying the rounding function independently to a vector of numbers does not, in general, preserve their sum. This function ensures that the sum is preserved after rounding.

Usage

1
rounded_sum(x, digits)

Arguments

x

A numeric vector where it is desired that each number be rounded to 'digits' digits.

digits

The number of digits where rounding is applied.

Details

It achieves this using the following algorithm:

  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.

Value

Returns a vector similar to x, except that the numbers have been rounded and their sum is preserved.


jeanmarcgp/xtsanalytics documentation built on May 19, 2019, 12:38 a.m.