Home / Blog

How to merge data in R using R merge, dplyr, or data.table

William Tsu
Data Analyst
Experienced data analyst working with data visualization, cloud computing and ETL solutions.
October 18, 2022

Data is never accessible in the desired configuration. Occasionally a join is cited as a merge and vice versa. Joining or merging refers to the procedure of conforming two data frames by either one or more pivotal variables i.e., horizontal merge, or by variable inscriptions or rows i.e., vertical merge. In the construction of a database, all the information cannot be documented in a single table to resist duplicacy. To take out unique data from the database, we integrate different or supplementary tables using a popular field. The joining brings lodging w.r.t the notion of the merge. There are multiple circumstances where data sets are halted into numerous tables and considerable justifications that this might make a point periodically that it is simpler to compile information in various pieces, and to different extents, it is to lessen the file extent. Nonetheless, the motive for dividing data sets into numerous tables should constantly be formatted in such a direction that there is at least one mutual column between the tables so that they can be assembled as required.

In R, extensively tabular sets of data are named data frames and the data frames can catalog matters of various classes as certain columns can be text and additional columns can be integers. A compatible phrase is a data table, which is utilized by some readers and different languages, particularly the structured query language (SQL). R has several sharp, sophisticated directions to enlist data frames by a popular column. To join two data frames or datasets horizontally, employ the merge method. In supreme trials, you engage two data frames by one or extra common code variables i.e., an inner join. We can integrate two data frames in R by wielding the merge () function or by utilizing the family of join() function in the dplyr batch and in addition to the join functions from the dplyr package encircled in the procedure, we can try the merge function from base R to execute a horizontal merge. Because this function succeeds from base R, we do not expect to introduce and access a different assortment as we do with the merge functions, which some may discover beneficial.

Merges with base R

The merge () Function in R is equal to the database merge system in SQL and the distinct assertions to merge() enable you to conduct natural merges i.e. inner join, left join, right join, cross join, semi join, anti join, and full outer join. The decree of data frame 1 and data frame 2 doesn't question, but whichever one is initial is contemplated x and the next one is y.

In Natural join or Inner Join, maintain just rows that match from the data frames, stipulate the statement all=FALSE, and in the case of Full outer join or Outer Join, retain all rows from both data frames, prescribe all=TRUE. Talking about the Left outer join or Left Join, encompass all the rows of your data frame x and merely those from y that contest, define x=TRUE and when in Right outer join or Right Join, comprise all the rows of your data frame y and hardly those from x that match, determine y=TRUE.

Merges with dplyr

Frequently when functioning with disparate datasets that are possibly sent out from a database or standalone you might need to merge the data concurrently on a broad key or column. This can generally seize a spot within a database, but if you don’t possess authorization or concern to do so, or don’t expect to ETL for one-off estimation, then employing dplyr and R to enlist the data can substantiate to be further productive. The dplyr assortment utilizes SQL database syntax for its merge objectives. A left join implicates encompassing everything on the left, what was the x data frame in merge(), and all rows that correspond from the right (y) data frame. A left join keeps every row in the left data frame and exclusively conforms rows from the right data frame.

dplyr perpetually maintains the row ordinance, has much more involuntary syntax and can be pertained to databases, or vitality. R has built-in toiling for the random count, but if you’re a dplyr user, it’s worth pointing out that the assortment amasses an arguably better aesthetically fascinating alternative than R’s insolvency. With dplyr, you can completely ratify the data and sample size as parameters to sample_n and the dplyr moreover authorizes you to model by a fraction, with the integrity of 0–1 demonstrating the fraction size, dplyr is also a front-end language for using data that can be transformed into numerous backends like SQL or spark.

Merges with data.table

To merge data.tables, the basic is the ON or USING clause is interpreted by outlining the solutions on the tables with setkey(), without anything else, TABLE_X[TABLE_Y] retreats a favorable outer join establishing nomatch=0 it subsides an inner join. The data.table package is generously recognized for its speed, so it can be a promising option for marketing with huge data sets. Practically all merge between 2 data.tables employ an inscription where one of them is utilized as i in a frame applied to the distinct, and the enlisting columns are defined with the on parameter. Nevertheless, in addition to the “basic” merges, data.table supposes unique trials like rolling merges, condensing while joining, non-equi merges, etc.

While dplyr has extremely relaxed and instinctive syntax, the data.table can be ordinances of magnitude sooner in some systems. Employing dtplyr compels understanding approximately no extra code. One commences data.table progression utilizing the lazy_dt task, after which formal dplyr code is composed. You can perform that with the dtplyr assortment, which is excellent for individuals who prefer dplyr syntax, or who are used to SQL database syntax but need quick data.table execution. data.table code is the quickest and dtplyr is nearly as fast, dplyr will carry about twice as extended, and base R is nearly 15 or 20 times stagnant as per research. The rendition relies on the configuration and size of the data and can alter incredibly pivoting on a specific chore. But it’s prudent to explain that base R isn’t a tremendous option for massive data sets.

What ensues is a rapid comparison of these packages integrating all the responses from the manipulator bands and it can moreover be beneficial to search for rows in a data set that didn’t approximate since that can support you discern the constraints of your information and whether something you anticipated is forfeiting. In dplyr, discovering rows that didn’t correspond is an anti-join and an Anti join maintains rows from the left data frame without a relay. While merge() syntax is somewhat lenient for supreme categories of joins, in this possibility, it brings a bit complicated if you need rows without a contest. To merge data frames on multiple columns in R utilizing either the base merge() procedure or the use of dplyr tasks. Operating the dplyr roles is a reasonable attitude as it rides quicker than the R base technique. dplyr assortment furnishes different capacities to merge R data frames and all these benefits merge on multiple columns. data.table has a couple of strategies to set considerable evidence in a data set. There’s setkey() to cite column titles unquoted, and setkeyv() if you wish the terms cited in a vector, beneficial for when this mission is within a purpose. Data.table is a strong contemporary update of the esteemed old data.frame. Under the hood, the assortment has been pitched for streaking rate and least memory mode with a syntax that is smart and reserved. As you’ll see when you examine the authorized documentation, the developers of the data.table package motivates users to speculate about governing particular tables much as you would speculate about questioning a relational database. Still, it is likewise continually the outbreak that you desire to merge tables by two or more columns.


In the real world, data may appear split over multiple datasets, across numerous unique formats. Because R is formulated to operate with sole tables of information, manipulating and condensing datasets into an available table is a fundamental mastery. In the applied arena, data are hosted on distinct servers and prevail in assorted various records. When the data you require come from numerous references, it's vital to understand how to aggregate them so that you lose as slight data as feasible and bring about pairings that render significance bestowed the system of your data. At the high level, there are two kinds you can join datasets; you can enhance data by strengthening additional rows or by developing more columns for your dataset. In common, when you have datasets that possess a compatible gear of columns or retain the equivalent set of statements, you can conjoin them vertically or horizontally, respectively. There are myriad types of merges and you can comprehend how to improve columns from one dataset with columns from another with mutating joins, how to seep one dataset against another with filtering merges, and how to refine through datasets with set undertakings in the merging data in R with dplyr.