# Merging in R

## 2005-07-26

Merging is tricky in any program, and you have to be sure that you know what you are doing. I have found that keeping an index variable will allow me to backtrack and find information in the original files if needed. Here’s a simple example of how this would work.

# randomly select 20 letters without replacement.
x0 <- sample(letters,20)
x1 <- sample(letters,20)
x0
##  [1] "b" "j" "m" "a" "g" "c" "h" "v" "k" "u" "i" "n" "e" "s" "l" "o" "f" "r" "w"
## [20] "x"
x1
##  [1] "b" "s" "i" "q" "e" "k" "r" "j" "d" "a" "z" "u" "n" "t" "p" "g" "x" "m" "v"
## [20] "l"

The intersect functions lets us know which letters are in both x0 and x1.

intersect(x0,x1)
##  [1] "b" "j" "m" "a" "g" "v" "k" "u" "i" "n" "e" "s" "l" "r" "x"

The setdiff function lets us know which letters are in x0 but not x1 (reverse the arguments to get letters in x1 but not in x1).

setdiff(x0,x1)
## [1] "c" "h" "o" "f" "w"
setdiff(x1,x0)
## [1] "q" "d" "z" "t" "p"

By default, the merge function includes only those values in common with the two files. Note that the id1 and id2 values tells us that the first row of x2 comes from the 19th and 8th rows of x0 and x1, respectively. Notice also that the merged file is sorted by the intersection of the two files.

x2 <- merge(list(x=x0,id0=1:20),list(x=x1,id1=1:20))
x2
##    x id0 id1
## 1  a   4  10
## 2  b   1   1
## 3  e  13   5
## 4  g   5  16
## 5  i  11   3
## 6  j   2   8
## 7  k   9   6
## 8  l  15  20
## 9  m   3  18
## 10 n  12  13
## 11 r  18   7
## 12 s  14   2
## 13 u  10  12
## 14 v   8  19
## 15 x  20  17

To include all values, including mismatches, specify all=T. Notice now that the merged file is sorted by the values of x0, followed by the sorted values of x1 that are not found in x0. An NA for an index value tells you that the letter is not found in the original x0 or x1 vector.

x3 <- merge(list(x=x0,id0=1:20),list(x=x1,id1=1:20),all=T)
x3
##    x id0 id1
## 1  a   4  10
## 2  b   1   1
## 3  c   6  NA
## 4  d  NA   9
## 5  e  13   5
## 6  f  17  NA
## 7  g   5  16
## 8  h   7  NA
## 9  i  11   3
## 10 j   2   8
## 11 k   9   6
## 12 l  15  20
## 13 m   3  18
## 14 n  12  13
## 15 o  16  NA
## 16 p  NA  15
## 17 q  NA   4
## 18 r  18   7
## 19 s  14   2
## 20 t  NA  14
## 21 u  10  12
## 22 v   8  19
## 23 w  19  NA
## 24 x  20  17
## 25 z  NA  11

The %in% operator is also useful. The %in% operator compares each element of the first set against all the elements of the second set and returns TRUE if there is a match.

x0 %in% x1
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13]  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE

This function is not symmetric, of course,

x1 %in% x0
##  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
## [13]  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

Using the %in% function, you can re-create the intersect function,

x0[x0 %in% x1]
##  [1] "b" "j" "m" "a" "g" "v" "k" "u" "i" "n" "e" "s" "l" "r" "x"

or the setdiff function,

x0[!x0 %in% x1]
## [1] "c" "h" "o" "f" "w"

A less intuitive version of the %in% operator is the match function. Additional information on merging can be found at

You can find an earlier version of this page on my original website.