@@ -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_by_x = setdiff(by.x , nm_x )
40+ stopf(" Elements listed in `by.x` must be valid column names in x. Missing: %s" , paste(missing_by_x , collapse = " , " ))
41+ }
42+ if (! all(by.y %chin % nm_y )) {
43+ missing_by_y = setdiff(by.y , nm_y )
44+ stopf(" Elements listed in `by.y` must be valid column names in y. Missing: %s" , paste(missing_by_y , collapse = " , " ))
45+ }
4246 by = by.x
4347 names(by ) = by.y
4448 } else {
@@ -50,8 +54,12 @@ 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_in_x = setdiff(by , nm_x )
59+ missing_in_y = setdiff(by , nm_y )
60+ stopf(" Elements listed in `by` must be valid column names in x and y. Missing in x: %s. Missing in y: %s" ,
61+ paste(missing_in_x , collapse = " , " ), paste(missing_in_y , collapse = " , " ))
62+ }
5563 by = unname(by )
5664 by.x = by.y = by
5765 }
0 commit comments