What do by.x and by.y do in merge()?

You have to pass the column names as a vector (or for one column, as a string surrounded by quotes), not just by specifying them directly. See below:

xx <- read.table(text = "name1 math
1 a 1
2 b 2
3 c 3
4 d 8",
                 header = TRUE,
                 stringsAsFactors = FALSE)

yy <- read.table(text = "name2 english
1 c 4
2 b 5
3 a 6
4 e 9",
                 header = TRUE,
                 stringsAsFactors = FALSE)

merge(x = xx,
      y = yy,
      by.x = "name1",
      by.y = "name2")
#>   name1 math english
#> 1     a    1       6
#> 2     b    2       5
#> 3     c    3       4

Created on 2020-02-23 by the reprex package (v0.3.0)

Depending on you expected output, you have to use the all, or all.x or all.y arguments.

Hope this helps.

1 Like