R - Join Data Frame (Merge)

1 - About

3 - 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.

4 - Join

4.1 - Inner

4.2 - Outer

4.2.1 - Left outer

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

4.2.2 - Right outer

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

4.2.3 - 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)

4.3 - Cross

Cross join:

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

5 - Documentation / Reference

lang/r/join.txt · Last modified: 2017/09/13 16:15 by gerardnico