<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-match_order-to-align-data/"></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/04/r-tip-use-match_order-to-align-data/"> 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">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>. Use <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/match_order.html" target="_blank"><code>wrapr::match_order()</code></a> to align data.</p>
<p><span></span></p>
<p>
<p>Suppose we have data in two data frames, and both of these data frames have common row-identifying columns called “<code>idx</code>“.</p>
<pre>
library(&quot;wrapr&quot;)

d1 &lt;- build_frame(
   &quot;idx&quot;, &quot;x&quot; |
   3    , &quot;a&quot; |
   1    , &quot;b&quot; |
   2    , &quot;c&quot; )

d2 &lt;- build_frame(
   &quot;idx&quot;, &quot;y&quot; |
   2    , &quot;D&quot; |
   1    , &quot;E&quot; |
   3    , &quot;F&quot; )

print(d1)
#>   idx x
#> 1   3 a
#> 2   1 b
#> 3   2 c

print(d2)
#>   idx y
#> 1   2 D
#> 2   1 E
#> 3   3 F
</pre>
<p>(Please see <a rel="nofollow" href="http://www.win-vector.com/blog/2018/04/r-tip-think-in-terms-of-values/" target="_blank">R Tip: Think in Terms of Values</a> for <code>build_frame()</code> and other value capturing tools.)</p>
<p>Often we wish to work with such data aligned so each row in <code>d2</code> has the same <code>idx</code> value as the same row (by row order) as <code>d1</code>. This is an important data wrangling task, so there are many ways to achieve it in R, such as <code>base::merge()</code>, <code>dplyr::left_join()</code>, or by sorting both tables into the same order and then using <code>base::cbind()</code>.</p>
<p>However if you wish to preserve the order of the first table (which may not be sorted), you need one more trick.</p>
<p>You can add a row-id column, sort by the joining id, combine and then re-sort by the row-id column.</p>
<p>Or you can match the orders in one step using <code>wrapr::match_order()</code>.</p>
<pre>
p &lt;- match_order(d2$idx, d1$idx)

print(d2[p, , drop=FALSE])
#>   idx y
#> 3   3 F
#> 2   1 E
#> 1   2 D
</pre>
<p><code>match_order</code> is merely wrapping all of the sort and re-sort tricks we mentioned above, however the theory is based on the <a rel="nofollow" href="http://www.win-vector.com/blog/2017/05/on-indexing-operators-and-composition/" target="_blank">absolute magic of associative array indexing</a>.</p>
<p>Please see <a rel="nofollow" href="http://www.win-vector.com/blog/2018/04/r-tip-use-match_order-to-align-data/" target="_blank">R Tip: Use <code>drop = FALSE</code> with <code>data.frame</code>s</a>, for why one should get in the habit of writing <code>drop = FALSE</code>.</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/04/r-tip-use-match_order-to-align-data/"> 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/UDgFLFBlJ8I" height="1"></a></div>