Joining Data in R

We can merge two data frames in R by using the merge() function or by using family of join() function in dplyr package. The data frames must have same column names on which the merging happens. Merge() Function in R is similar to database join operation in SQL. The different arguments to merge() allow you to perform natural joins i.e. inner join, left join, right join,cross join, semi join, anti join and full outer join. We can perform Join in R using merge() Function or by using family of join() functions in dplyr package.

Joins Diagram

Load Packages

packages <- c("tidyverse", "fuzzyjoin")

packages <- lapply(packages, FUN = function(x) {

if(!require(x, character.only = TRUE)) {install.packages(x)

library(x, character.only = TRUE)}})

Create diamonds dataframe

diamonds <- data.frame(diamonds)

Create Test Data Frame

match <- data_frame(match = c("^Idea", "mium", "Good"), type = 1:3)

Inner Join

With regular inner join, only Good <-> Good matches

inner.join <- diamonds %>% inner_join(match, by = c(cut = "match"))

nrow(inner.join)

Full Join

With regular full join, only Good <-> Good matches

full.join <- diamonds %>% join(match, by = c(cut = "match"))

nrow(full.join)

Left Join

With regular left join, only Good <-> Good matches

left.join <- diamonds %>% left_join(match, by = c(cut = "match"))

nrow(left.join)

Full Join

With regular full join, only Good <-> Good matches

full.join <- diamonds %>% join(match, by = c(cut = "match"))

nrow(full.join)

Right Join

With regular right join, only Good <-> Good matches

right.join <- diamonds %>% right_join(match, by = c(cut = "match"))

nrow(right.join)

Regex Inner Join

inner.join.regex <- diamonds %>% regex_inner_join(match, by = c(cut = "match"))

nrow(inner.join.regex)

Regex Left Join

left.join <- regex_left_join(df, df.join, by = "join.column", ignore_case = FALSE)

Regex Right Join

right.join <- regex_right_join(df, df.join, by = "join.column", ignore_case = FALSE)

References:

Sabalico Logo
Sabalytics Logo
World Map Logo
rStatistics Logo
Time Zone Logo
Galaxy View Logo
Periodic Table Logo
My Location Logo
Weather Track Logo
Sprite Sheet Logo
Barcode Generator Logo
Test Speed Logo
Website Tools Logo
Image Tools Logo
Color Tools Logo
Text Tools Logo
Finance Tools Logo
File Tools Logo
Data Tools Logo
History of Humanity - History Archive Logo
History of Humanity - History Mysteries Logo
History of Humanity - Ancient Mesopotamia Logo
History of Humanity - Egypt History Logo
History of Humanity - Persian Empire Logo
History of Humanity - Greek History Logo
History of Humanity - Alexander the Great Logo
History of Humanity - Roman History Logo
History of Humanity - Punic Wars Logo
History of Humanity - Golden Age of Piracy Logo
History of Humanity - Revolutionary War Logo