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] "r" "l" "s" "a" "p" "x" "m" "b" "v" "u" "w" "j" "c" "z" "k" "i" "q" "n" "y"
## [20] "o"
```

`x1`

```
## [1] "z" "v" "m" "p" "j" "l" "g" "a" "q" "b" "n" "t" "c" "w" "u" "x" "e" "k" "y"
## [20] "s"
```

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

`intersect(x0,x1)`

`## [1] "l" "s" "a" "p" "x" "m" "b" "v" "u" "w" "j" "c" "z" "k" "q" "n" "y"`

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] "r" "i" "o"`

`setdiff(x1,x0)`

`## [1] "g" "t" "e"`

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 8
## 2 b 8 10
## 3 c 13 13
## 4 j 12 5
## 5 k 15 18
## 6 l 2 6
## 7 m 7 3
## 8 n 18 11
## 9 p 5 4
## 10 q 17 9
## 11 s 3 20
## 12 u 10 15
## 13 v 9 2
## 14 w 11 14
## 15 x 6 16
## 16 y 19 19
## 17 z 14 1
```

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 8
## 2 b 8 10
## 3 c 13 13
## 4 e NA 17
## 5 g NA 7
## 6 i 16 NA
## 7 j 12 5
## 8 k 15 18
## 9 l 2 6
## 10 m 7 3
## 11 n 18 11
## 12 o 20 NA
## 13 p 5 4
## 14 q 17 9
## 15 r 1 NA
## 16 s 3 20
## 17 t NA 12
## 18 u 10 15
## 19 v 9 2
## 20 w 11 14
## 21 x 6 16
## 22 y 19 19
## 23 z 14 1
```

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] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [13] TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE
```

This function is not symmetric, of course,

`x1 %in% x0`

```
## [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE
## [13] TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
```

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

`x0[x0 %in% x1]`

`## [1] "l" "s" "a" "p" "x" "m" "b" "v" "u" "w" "j" "c" "z" "k" "q" "n" "y"`

or the setdiff function,

`x0[!x0 %in% x1]`

`## [1] "r" "i" "o"`

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.