smartround | R Documentation |
Rounds the values in a vector up and down, preserving the sum of the vector and minimizing the total rounding error under this condition. An example where this is useful is when rounding a vector of percentages, where the total should add up to 100 percent.
smartround(x, digits = 0)
x |
vector of values |
digits |
optional non-negative integer specifying the number of digits of the rounded numbers |
Returns a vector of rounded values of the same length as x
.
Danail Obreschkow
x = runif(5)
x = x/sum(x)*100
print(x)
print(sum(x))
y = smartround(x)
print(y)
print(sum(y))
y2 = smartround(x,2)
print(y2)
print(sum(y2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.