Produce an integer vector approximation of a non-increasing non-negative numeric vector such that it sums to an positive integer

int_approx(vec, n)

Arguments

vec

non-decreasing non-negative numeric vector

n

A positive integer to which vector should sum to. Default value is the rounded sum of the vector

Value

An integer vector

Details

The numeric vector is scaled to sum to n. Then, an integer vector is assigned the integer parts of the scaled vector. The leftout part of the sum is distributed based on the magnitude of the fractional part. This kind of approximation is useful when an algorithm produces ranking scores which have to allocated with positive integer constraints.

Examples

int_approx(c(3.7, 3.2, 2.4, 1.5))
#> [1] 4 3 2 2