merge

merge(x, y, all=FALSE)
Merge two data frames together. The use described here assumes that the two data frames being merged have one common column name. The Tip describes how to handle other scenarios.
  • x – Data frame to be merged with y
  • y – Data frame to be merged with x
  • all – Indicate whether to include all rows from both data frames. The default (FALSE) specifies that only the rows that share a common ID in the overlapping column should be included.

Example. Two fake data sets d1 and d2 are created. The cases corresponding to ID 2 and 7 are represented in both data sets while all other IDs are unique to either d1 or d2.
> id1     <- c(2, 3, 4, 5, 7)
> heights <- c(62, 65, 71, 71, 67)
> d1      <- data.frame(id=id1, heights)
> 
> id2     <- c(1, 2, 6, 7, 8, 9, 10)
> weights <- c(147, 113, 168, 135, 144, 173, 128)
> d2      <- data.frame(id=id2, weights)
> 
> merge(d1, d2)
  id heights weights
1  2      62     113
2  7      67     135
> 
> merge(d1, d2, all=TRUE)
   id heights weights
1   1      NA     147
2   2      62     113
3   3      65      NA
4   4      71      NA
5   5      71      NA
6   6      NA     168
7   7      67     135
8   8      NA     144
9   9      NA     173
10 10      NA     128
Tip. The by argument should be used to control how the merging occurs when two or more column names in the data frames overlap.

Leave a Reply