Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] diagnostic for merge.data.table when by = key is not present in dt being merged #6556

Open
zhizhongpu opened this issue Oct 4, 2024 · 2 comments

Comments

@zhizhongpu
Copy link

zhizhongpu commented Oct 4, 2024

# Minimal reproducible example; please be sure to set verbose=TRUE where possible!

When merging two dt using merge.data.table and the key is not present in one of the two dts, data.table throws out an uninformative error:

Error in merge.data.table(test, manual, by = c("iso3c", "year"), all = T) : 
  Elements listed in `by` must be valid column names in x and y

which is not helpful as the user would then have to debug which key is not present in which dt. A more informative error would list all keys not present in all dts. For example:

> combined = test |> 
+   left_join(manual, by = c('iso3c', 'year')) 
Error in `left_join()`:
! Join columns in `y` must be present in the
  data.
✖ Problem with `iso3c`.

# Output of sessionInfo()

@zhizhongpu zhizhongpu changed the title [feature requestdiagnostic for merge.data.table when by = key is not present in dt being merged [feature request] diagnostic for merge.data.table when by = key is not present in dt being merged Oct 4, 2024
@MichaelChirico
Copy link
Member

Great idea, thanks! Here's the relevant error:

data.table/R/merge.R

Lines 53 to 54 in b4538a0

if (!all(by %chin% intersect(nm_x, nm_y)))
stopf("Elements listed in `by` must be valid column names in x and y")

Do you want to have a go at writing a PR?

@zhizhongpu
Copy link
Author

thanks @MichaelChirico - that's a helpful pointer.

On fixing this, I'm a bit too time-constrained maybe one day I'll get it done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants