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] "s" "m" "u" "d" "y" "l" "t" "c" "h" "f" "q" "v" "k" "r" "i" "b" "p" "z" "g"
## [20] "e"
```

`x1`

```
## [1] "m" "y" "o" "f" "n" "c" "x" "e" "j" "q" "l" "v" "t" "b" "g" "z" "a" "d" "h"
## [20] "r"
```

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

`intersect(x0,x1)`

`## [1] "m" "d" "y" "l" "t" "c" "h" "f" "q" "v" "r" "b" "z" "g" "e"`

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] "s" "u" "k" "i" "p"`

`setdiff(x1,x0)`

`## [1] "o" "n" "x" "j" "a"`

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 b 16 14
## 2 c 8 6
## 3 d 4 18
## 4 e 20 8
## 5 f 10 4
## 6 g 19 15
## 7 h 9 19
## 8 l 6 11
## 9 m 2 1
## 10 q 11 10
## 11 r 14 20
## 12 t 7 13
## 13 v 12 12
## 14 y 5 2
## 15 z 18 16
```

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

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

This function is not symmetric, of course,

`x1 %in% x0`

```
## [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
## [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] "m" "d" "y" "l" "t" "c" "h" "f" "q" "v" "r" "b" "z" "g" "e"`

or the setdiff function,

`x0[!x0 %in% x1]`

`## [1] "s" "u" "k" "i" "p"`

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.