<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/data-table-is-really-good-at-sorting/"></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/data-table-is-really-good-at-sorting/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank"><code>data.table</code></a> <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> package is <em>really</em> good at sorting. Below is a comparison of it versus <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank"><code>dplyr</code></a> for a range of problem sizes.</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/08/present-2.png?resize=660%2C440" alt="Present 2" width="450" title="present-2.png" data-recalc-dims="1"></p>
<p><span></span></p>
<p>
<p>The graph is using a log-log scale (so things are very compressed). But <code>data.table</code> is routinely 7 times faster than <code>dplyr</code>. The ratio of run times is shown below.</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/08/present-3.png?resize=660%2C440" alt="Present 3" width="450" title="present-3.png" data-recalc-dims="1"></p>
<p>Notice on the above semi-log plot the run time ratio is growing roughly linearly. This makes sense: <code>data.table</code> uses a radix sort which has the potential to perform in near linear time (faster than the <code>n log(n)</code> lower bound known comparison sorting) for a range of problems (also we are only showing example sorting times, not worst-case sorting times).</p>
<p>In fact, if we divide the <code>y</code> in the above graph by <code>log(rows)</code> we get something approaching a constant.</p>
<p><img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/08/present-4.png?resize=660%2C440" alt="Present 4" width="450" title="present-4.png" data-recalc-dims="1"></p>
<p>The above is consistent with <code>data.table</code> not only being faster than <code>dplyr</code>, but also having a fundamentally different asymptotic running time.</p>
<p>Performance like the above is one of the reasons you should <em>strongly</em> consider <code>data.table</code> for your <code>R</code> projects.</p>
<p>All details of the timings can be found <a rel="nofollow" href="https://github.com/WinVector/rqdatatable/blob/master/extras/Sorting.md" target="_blank">here</a>.</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/data-table-is-really-good-at-sorting/"> 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/XlH_eqHno_M" height="1"></a></div>