Skip to content

Commit 36cc9bb

Browse files
committed
fixed #6556
1 parent e4b0bbb commit 36cc9bb

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

R/merge.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
3535
if (!is.null(by.x)) {
3636
if (length(by.x)==0L || !is.character(by.x) || !is.character(by.y))
3737
stopf("A non-empty vector of column names is required for `by.x` and `by.y`.")
38-
if (!all(by.x %chin% nm_x))
39-
stopf("Elements listed in `by.x` must be valid column names in x.")
40-
if (!all(by.y %chin% nm_y))
41-
stopf("Elements listed in `by.y` must be valid column names in y.")
38+
if (!all(by.x %chin% nm_x)) {
39+
missing_keys_x = setdiff(by.x, nm_x)
40+
stopf("Elements listed in `by.x` must be valid column names in x. Missing keys: %s", paste(missing_keys_x, collapse=", "))
41+
}
42+
if (!all(by.y %chin% nm_y)) {
43+
missing_keys_y = setdiff(by.y, nm_y)
44+
stopf("Elements listed in `by.y` must be valid column names in y. Missing keys: %s", paste(missing_keys_y, collapse=", "))
45+
}
4246
by = by.x
4347
names(by) = by.y
4448
} else {
@@ -50,8 +54,10 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
5054
by = intersect(nm_x, nm_y)
5155
if (length(by) == 0L || !is.character(by))
5256
stopf("A non-empty vector of column names for `by` is required.")
53-
if (!all(by %chin% intersect(nm_x, nm_y)))
54-
stopf("Elements listed in `by` must be valid column names in x and y")
57+
if (!all(by %chin% intersect(nm_x, nm_y))) {
58+
missing_keys = setdiff(by, intersect(nm_x, nm_y))
59+
stopf("Elements listed in `by` must be valid column names in both x and y. Missing keys: %s", paste(missing_keys, collapse=", "))
60+
}
5561
by = unname(by)
5662
by.x = by.y = by
5763
}

0 commit comments

Comments
 (0)