@@ -35,10 +35,12 @@ 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+ missing_in_x <- setdiff(by.x , nm_x )
39+ missing_in_y <- setdiff(by.y , nm_y )
40+ if (length(missing_in_x ) > 0 )
41+ stopf(" The following columns specified in `by.x` are missing in `x`: %s" , toString(missing_in_x ))
42+ if (length(missing_in_y ) > 0 )
43+ stopf(" The following columns specified in `by.y` are missing in `y`: %s" , toString(missing_in_y ))
4244 by = by.x
4345 names(by ) = by.y
4446 } else {
@@ -50,8 +52,13 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
5052 by = intersect(nm_x , nm_y )
5153 if (length(by ) == 0L || ! is.character(by ))
5254 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" )
55+ missing_in_x <- setdiff(by , nm_x )
56+ missing_in_y <- setdiff(by , nm_y )
57+ if (length(missing_in_x ) > 0 || length(missing_in_y ) > 0 ) {
58+ stopf(" Error in merge.data.table():\n %s\n %s" ,
59+ if (length(missing_in_x )) paste(" ! Missing in x:" , toString(missing_in_x )) else " " ,
60+ if (length(missing_in_y )) paste(" ! Missing in y:" , toString(missing_in_y )) else " " )
61+ }
5562 by = unname(by )
5663 by.x = by.y = by
5764 }
0 commit comments