R - Join Data Frame (Merge)

Card Puncher Data Processing

About

Join

Syntax

merge(x = df1, y = df2, by = "Name") 
merge(
   x = df1, 
   y = df2, 
   by = intersect(names(x), names(y)),
   by.x = by, 
   by.y = by, 
   all = FALSE, -- Shorthand for all.x and all.y
   all.x = all, 
   all.y = all,
   sort = TRUE,
   suffixes = c(".x",".y"), -- To make unique the columns names
   incomparables = NULL, 
   ...
)

where:

  • the “by” parameters specifies the join column. Default: join by common variable names
    • The by.x and by.y parameters must be used if the matching variables have different names
  • the “all” parameters specifies which non matching row is returned.
  • sort: Should the result be sorted on the by columns?

The default method coerces the arguments to data frames and calls the “data.frame” method.

Join

Inner

Equi Join

merge(df1, df2) 

Outer

Outer join:

Left outer

merge(x = df1, y = df2, by = "Name", all.x = TRUE)

Right outer

merge(x = df1, y = df2, by = "Name", all.y = TRUE)

Full OUter

merge(x = df1, y = df2, by = "Name", all.y = TRUE, all.x = TRUE)
# or
merge(x = df1, y = df2, by = "Name", all = TRUE)

Cross

Cross join:

merge(x = df1, y = df2, by = NULL)

Documentation / Reference







Share this page:
Follow us:
Task Runner