0

I am merging two data.tables (version 1.9.4)

library(data.table)

dt1 <- data.table(
  key1 = rep(c("A", "B"), each = 2),
  key2 = c("apple", "orange", "kiwi", "mango"),
  vals = 1:4
)
setkey(dt1, key1, key2)

dt2 <- data.table(
  key_one = rep(rev(c("A", "B")), each = 3),
  key_two = rev(c("apple", "orange", "kiwi", "mango", "fig", "lemon")),
  more_vals = 20:15
)
setkey(dt2, key_one, key_two)

This works as expected

dt1[dt2]

This returns an error

merge(dt1, dt2)
Error in merge.data.table(dt1, dt2) : 
A non-empty vector of column names for `by` is required.

Am I using merge incorrectly?

Ed G
  • 802
  • 1
  • 6
  • 19
  • 3
    `merge.data.table` method doesn't work when the columns names are different in the two data sets because it doesn't have `by.x` and `by.y` arguments. See [here](http://stackoverflow.com/questions/14069796/merging-data-tables-based-on-columns-names) and [here](https://github.com/Rdatatable/data.table/issues/637) – David Arenburg May 20 '15 at 13:05
  • OK. I thought the names didn't matter - only the order of the keys – Ed G May 20 '15 at 13:06
  • 3
    Only `merge.data.frame` allows for different names in `by.x` and `by.y`: `merge.data.frame(dt1, dt2, by.x = key(dt1), by.y = key(dt2), all.y = TRUE)`. This doesn't work for the `data.table` method. – shadow May 20 '15 at 13:08

0 Answers0