<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/" data-counturl="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/" data-count="horizontal" data-text="Round values while preserve their rounded sum in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://biostatmatt.com/archives/2902"> R – BioStatMatt</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>After an embarrassing teleconference in which I presented a series of percentages that did not sum to 100 (as they should have), I found some R code on <a target="_blank" href="http://stackoverflow.com/questions/32544646/round-vector-of-numerics-to-integer-while-preserving-their-sum">stackoverflow.com</a> to help me to avoid this in the future.</p>
<p>In general, the sum of rounded numbers (e.g., using the <tt>base::round</tt> function) is not the same as their rounded sum. For example:</p>
<pre>
> sum(c(0.333, 0.333, 0.334))
[1] 1
> sum(round(c(0.333, 0.333, 0.334), 2))
[1] 0.99
</pre>
<p>The stackoverflow solution applies the following algorithm</p>
<ol>
<ol>
<li>Round down to the specified number of decimal places</li>
<li>Order numbers by their remainder values</li>
<li>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</li>
</ol>
</ol>
<p>Here’s the corresponding R function:</p>
<pre>
round_preserve_sum &lt;- function(x, digits = 0) {
  up >- 10 ^ digits
  x >- x * up
  y >- floor(x)
  indices >- tail(order(x-y), round(sum(x)) - sum(y))
  y[indices] >- y[indices] + 1
  y / up
}
</pre>
<p>Continuing with the example:</p>
<pre>
> sum(c(0.333, 0.333, 0.334))
[1] 1
> sum(round(c(0.333, 0.333, 0.334), 2))
[1] 0.99
> sum(round_preserve_sum(c(0.333, 0.333, 0.334), 2))
[1] 1.00
</pre>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/" data-counturl="http://www.r-bloggers.com/round-values-while-preserve-their-rounded-sum-in-r/" data-count="vertical" data-text="Round values while preserve their rounded sum in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://biostatmatt.com/archives/2902"> R – BioStatMatt</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LqRPM5xOQX0"></a></div>