diff --git a/R/merge.R b/R/merge.R index ab93d5498..7892b3c57 100644 --- a/R/merge.R +++ b/R/merge.R @@ -35,10 +35,14 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL if (!is.null(by.x)) { if (length(by.x)==0L || !is.character(by.x) || !is.character(by.y)) stopf("A non-empty vector of column names is required for `by.x` and `by.y`.") - if (!all(by.x %chin% nm_x)) - stopf("Elements listed in `by.x` must be valid column names in x.") - if (!all(by.y %chin% nm_y)) - stopf("Elements listed in `by.y` must be valid column names in y.") + if (!all(by.x %chin% nm_x)) { + missing_keys_x = setdiff(by.x, nm_x) + stopf("Elements listed in `by.x` must be valid column names in x. Missing keys: %s", paste(missing_keys_x, collapse=", ")) + } + if (!all(by.y %chin% nm_y)) { + missing_keys_y = setdiff(by.y, nm_y) + stopf("Elements listed in `by.y` must be valid column names in y. Missing keys: %s", paste(missing_keys_y, collapse=", ")) + } by = by.x names(by) = by.y } else { @@ -50,8 +54,10 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL by = intersect(nm_x, nm_y) if (length(by) == 0L || !is.character(by)) stopf("A non-empty vector of column names for `by` is required.") - if (!all(by %chin% intersect(nm_x, nm_y))) - stopf("Elements listed in `by` must be valid column names in x and y") + if (!all(by %chin% intersect(nm_x, nm_y))) { + missing_keys = setdiff(by, intersect(nm_x, nm_y)) + stopf("Elements listed in `by` must be valid column names in both x and y. Missing keys: %s", paste(missing_keys, collapse=", ")) + } by = unname(by) by.x = by.y = by }