<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-tip-use-radix-sort/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/08/r-tip-use-radix-sort/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>: use radix sort.</p>
<p><span></span></p>
<p>
<p>The “<code><span>method =</span> <span>&quot;radix&quot;</span></code>” option can <em>greatly</em> speed up sorting and ordering tables in <code>R</code>.</p>
<p>For a 1 million row table the speedup is already as much as 35 times (around 9.6 seconds versus 3 tenths of a second). Below is an excerpt from an experiment sorting showing default settings and showing radix sort (full code <a rel="nofollow" href="https://github.com/WinVector/rqdatatable/blob/master/extras/radix_timing.md" target="_blank">here</a>).</p>
<div>
<pre><code><span>
timings &lt;-<span> </span><span>microbenchmark</span>(
<span>order_default =</span> d[<span>order</span>(d<span>$</span>col_a, d<span>$</span>col_b, d<span>$</span>col_c, d<span>$</span>col_x), , <span>drop =</span> <span>FALSE</span>],
<span>order_radix =</span> d[<span>order</span>(d<span>$</span>col_a, d<span>$</span>col_b, d<span>$</span>col_c, d<span>$</span>col_x,
<span>method =</span> <span>&quot;radix&quot;</span>), ,
<span>drop =</span> <span>FALSE</span>],
<span>check =</span> my_check,
<span>times =</span> 10L)
<span>print</span>(timings)

<pre><code>## Unit: milliseconds
##           expr       min        lq      mean    median        uq
##  order_default 9531.2865 9653.6827 9759.8929 9690.6702 9833.2170
##    order_radix  262.1377  263.3226  278.2547  265.1452  274.2476
##         max neval
##  10329.3520    10
##    382.2544    10</code></pre>

<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/08/unnamed-chunk-1-1.png?resize=660%2C471" alt="Unnamed chunk 1 1" width="450" title="unnamed-chunk-1-1.png" data-recalc-dims="1">
</div>
<p>This speedup is possible because Matt Dowle and Arun Srinivasan of the <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank"><code>data.table</code></a> team generously ported their radix sorting code into base-<code>R</code>! Please see <code>help(sort)</code> for details. So <code>data.table</code> is not only the best data manipulation package in <code>R</code>, the team actually works to improve <code>R</code> itself. This is what is meant by &quot;<code>R</code> community&quot; and what is needed to keep <code>R</code> vibrant and alive.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2018/08/r-tip-use-radix-sort/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/UFgP5V5aSRk" height="1"></a></div></span></code></pre></div>