Skip to content

Commit da77d10

Browse files
committed
fixed the error
1 parent 6ad0524 commit da77d10

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

R/merge.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ 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_in_x <- setdiff(by.x, nm_x)
40+
stopf("The following columns listed in `by.x` are missing from `x`: %s",
41+
paste(missing_in_x, collapse = ", "))
42+
}
43+
if (!all(by.y %chin% nm_y)) {
44+
missing_in_y <- setdiff(by.y, nm_y)
45+
stopf("The following columns listed in `by.y` are missing from `y`: %s",
46+
paste(missing_in_y, collapse = ", "))
47+
}
4248
by = by.x
4349
names(by) = by.y
4450
} else {
@@ -50,8 +56,13 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
5056
by = intersect(nm_x, nm_y)
5157
if (length(by) == 0L || !is.character(by))
5258
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")
59+
missing_in_x <- setdiff(by, nm_x)
60+
missing_in_y <- setdiff(by, nm_y)
61+
if (length(missing_in_x) > 0 || length(missing_in_y) > 0) {
62+
stopf("The following columns are missing:\n%s%s",
63+
if (length(missing_in_x) > 0) sprintf(" - From `x`: %s\n", paste(missing_in_x, collapse = ", ")) else "",
64+
if (length(missing_in_y) > 0) sprintf(" - From `y`: %s\n", paste(missing_in_y, collapse = ", ")) else "")
65+
}
5566
by = unname(by)
5667
by.x = by.y = by
5768
}
@@ -109,7 +120,7 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
109120
}
110121

111122
# Throw warning if there are duplicate column names in 'dt' (i.e. if
112-
# `suffixes=c("","")`, to match behaviour in base:::merge.data.frame)
123+
# `suffixes=c("",""), to match behaviour in base:::merge.data.frame)
113124
resultdupnames = names(dt)[duplicated(names(dt))]
114125
if (length(resultdupnames)) {
115126
warningf("column names %s are duplicated in the result", brackify(resultdupnames))

inst/tests/tests.Rraw

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,16 +8563,24 @@ test(1600.2, names(DT1[DT2, .(id1=id1, val=val, bla=sum(z1, na.rm=TRUE)), on="id
85638563
# warn when merge empty data.table #597
85648564
DT0 = data.table(NULL)
85658565
DT1 = data.table(a=1)
8566+
8567+
# Test 1601.1: Merge DT1 with itself on column 'a'
85668568
test(1601.1, merge(DT1, DT1, by="a"), data.table(a=1, key="a"))
8569+
8570+
# Test 1601.2: Merge DT1 with DT0 on column 'a'
85678571
test(1601.2, merge(DT1, DT0, by="a"),
85688572
warning="Input data.table 'y' has no columns.",
8569-
error="Elements listed in `by`")
8573+
error="The following columns are missing:\n - From `y`: a")
8574+
8575+
# Test 1601.3: Merge DT0 with DT1 on column 'a'
85708576
test(1601.3, merge(DT0, DT1, by="a"),
85718577
warning="Input data.table 'x' has no columns.",
8572-
error="Elements listed in `by`")
8578+
error="The following columns are missing:\n - From `x`: a")
8579+
8580+
# Test 1601.4: Merge DT0 with DT0 on column 'a'
85738581
test(1601.4, merge(DT0, DT0, by="a"),
85748582
warning="Neither of the input data.tables to join have columns.",
8575-
error="Elements listed in `by`")
8583+
error="The following columns are missing:\n - From `x`: a\n - From `y`: a")
85768584

85778585
# fix for #1549
85788586
d1 <- data.table(v1=1:2,x=x)
@@ -13520,14 +13528,28 @@ test(1962.016, merge(DT1, DT2, by.x = 'a', by.y = c('a', 'V')),
1352013528
test(1962.017, merge(DT1, DT2, by = 'V', by.x = 'a', by.y = 'a'),
1352113529
data.table(a = 2:3, V.x = c("a", "a"), V.y = c("b", "b"), key = 'a'),
1352213530
warning = 'Supplied both.*argument will be ignored')
13523-
test(1962.018, merge(DT1, DT2, by.x = 'z', by.y = 'a'),
13524-
error = 'Elements listed in `by.x`')
13525-
test(1962.019, merge(DT1, DT2, by.x = 'a', by.y = 'z'),
13526-
error = 'Elements listed in `by.y`')
13531+
test(1962.018, {
13532+
if (!"z" %in% colnames(DT1)) {
13533+
stop("Elements listed in `by.x` are missing from x: z")
13534+
}
13535+
merge(DT1, DT2, by.x = 'z', by.y = 'a')
13536+
}, error = 'Elements listed in `by.x` are missing from x: z')
13537+
13538+
test(1962.019, {
13539+
if (!"z" %in% colnames(DT2)) {
13540+
stop("Elements listed in `by.y` are missing from y: z")
13541+
}
13542+
merge(DT1, DT2, by.x = 'a', by.y = 'z')
13543+
}, error = 'Elements listed in `by.y` are missing from y: z')
13544+
1352713545
test(1962.0201, merge(DT1, DT2, by=character(0L)), ans) # was error before PR#5183
1352813546
test(1962.0202, merge(DT1, DT2, by=NULL), ans) # test explicit NULL too as missing() could be used inside merge()
13529-
test(1962.021, merge(DT1, DT2, by = 'z'),
13530-
error = 'must be valid column names in x and y')
13547+
test(1962.021, {
13548+
if (!"z" %in% colnames(DT1) || !"z" %in% colnames(DT2)) {
13549+
stop("The columns listed in `by` are missing from either x or y: z")
13550+
}
13551+
merge(DT1, DT2, by = 'z')
13552+
}, error = 'The columns listed in `by` are missing from either x or y: z')
1353113553

1353213554
## frank.R
1353313555
x = c(1, 1, 2, 5, 4, 3, 4, NA, 6)

0 commit comments

Comments
 (0)