<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/union-multiple-data-frames-with-different-column-names/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statcompute.wordpress.com/2018/09/22/union-multiple-data-frames-with-different-column-names/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>On Friday, while working on a project that I needed to union multiple data.frames with different column names, I realized that the base::rbind() function doesn’t take data.frames with different columns names and therefore just quickly drafted a rbind2() function on the fly to get the job done based on the idea of MapReduce that I discussed before (<a rel="nofollow" href="https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting" target="_blank">https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting</a>). </p>
<pre>
rbind2 &lt;- function(lst) {
  h &lt;- unique(unlist(lapply(lst, names)))
  Reduce(rbind, parallel::mcMap(function(x) {x[, setdiff(h, names(x))] &lt;- NA; return(x)}, lst, mc.cores = length(lst)))
}
</pre>
<p>On Saturday, when I revisited the problem, I found a very good thread on the stackoverflow (<a rel="nofollow" href="https://stackoverflow.com/questions/3402371/combine-two-data-frames-by-rows-rbind-when-they-have-different-sets-of-columns" target="_blank">https://stackoverflow.com/questions/3402371/combine-two-data-frames-by-rows-rbind-when-they-have-different-sets-of-columns</a>) discussing various approaches addressing my problem yesterday. Out of curiosity, I did a comparison between the rbind2() and discussed approaches by combining 8 data.frames each with a million records. As shown in the plot, my homebrew rbind2() function is only marginally faster than the gtools::smartbind() function and the dplyr::bind_rows function is the most efficient. </p>
<pre>
n &lt;- 1000000
d1 &lt;- data.frame(id = 1:n, x1 = 1)
d2 &lt;- data.frame(id = 1:n, x2 = 2)
d3 &lt;- data.frame(id = 1:n, x3 = 3)
d4 &lt;- data.frame(id = 1:n, x4 = 4)
d5 &lt;- data.frame(id = 1:n, x5 = 5)
d6 &lt;- data.frame(id = 1:n, x6 = 6)
d7 &lt;- data.frame(id = 1:n, x7 = 7)
d8 &lt;- data.frame(id = 1:n, x8 = 8)
microbenchmark::microbenchmark(times = 10, 
  &quot;homebrew::rbind2&quot;      = {rbind2(list(d1, d2, d3, d4, d5, d6, d7, d8))},
  &quot;gtools::smartbind&quot;     = {gtools::smartbind(list = list(d1, d2, d3, d4, d5, d6, d7, d8))},
  &quot;dplyr::bind_rows&quot;      = {dplyr::bind_rows(d1, d2, d3, d4, d5, d6, d7, d8)},
  &quot;plyr::rbind.fill&quot;      = {plyr::rbind.fill(d1, d2, d3, d4, d5, d6, d7, d8)},
  &quot;data.table::rbindlist&quot; = {data.table::rbindlist(list(d1, d2, d3, d4, d5, d6, d7, d8), fill = T)}
)
</pre>
<p><img data-orig-file="https://statcompute.files.wordpress.com/2018/09/rplot1.png?w=456" data-orig-size="1098,523" data-medium-file="https://statcompute.files.wordpress.com/2018/09/rplot1.png?w=456?w=300" data-attachment-id="3238" data-image-meta="{" data-image-description="" src="https://statcompute.files.wordpress.com/2018/09/rplot1.png?w=456" data-permalink="https://statcompute.wordpress.com/2018/09/22/union-multiple-data-frames-with-different-column-names/rplot-3/" data-large-file="https://statcompute.files.wordpress.com/2018/09/rplot1.png?w=456?w=1098" alt="Rplot" data-image-title="Rplot" sizes="(max-width: 1098px) 100vw, 1098px" data-comments-opened="0" class="wp-image-3238" data-recalc-dims="1"></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="https://statcompute.wordpress.com/2018/09/22/union-multiple-data-frames-with-different-column-names/"> S+/R – Yet Another Blog in Statistical Computing</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gHdbiEZBrlk" height="1"></a></div>