Silently in-place merge last row with on= when multiple rows match

Open
#3,747 6 comments 3 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #4370 by @jangorecki — merged

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Re-run the minimal example using d1[d2, y := y, on = "id"] and trace how duplicate matches are handled during the in-place update. Review linked pull request #4370 for work already attempted, then identify the relevant join-update tests or entry points; done should include a defined signal for multiple matches.

Written by the indexing model from the issue text.

Description

joins

data[dt, x := y, on = "group"] performs in-place adding columns from dt to data on corresponding group. Currently, if dt has multiple rows that match a certain group, it seems the last row in that group will be taken, silently. I'm not sure if it is a designed behavior, but for me such multiple matching in most cases is simply a mistake and I should take a closer look at why dt has multiple matches on group, which often suggests that I made a mistake creating dt or something is missing in dt.

A minimal example is following:

library(data.table)

d1 <- data.table(id = 1:10, x = 1:10)
d1
#>     id  x
#>  1:  1  1
#>  2:  2  2
#>  3:  3  3
#>  4:  4  4
#>  5:  5  5
#>  6:  6  6
#>  7:  7  7
#>  8:  8  8
#>  9:  9  9
#> 10: 10 10
d2 <- data.table(id = c(1, 1, 2, 3, 3), y = c(1, 2, 2, 3, 4))
d2
#>    id y
#> 1:  1 1
#> 2:  1 2
#> 3:  2 2
#> 4:  3 3
#> 5:  3 4
d1[d2, y := y, on = "id"]
d1
#>     id  x  y
#>  1:  1  1  2
#>  2:  2  2  2
#>  3:  3  3  4
#>  4:  4  4 NA
#>  5:  5  5 NA
#>  6:  6  6 NA
#>  7:  7  7 NA
#>  8:  8  8 NA
#>  9:  9  9 NA
#> 10: 10 10 NA

Would it make sense if a signal is generated for such case so that the problem can be spotted eariler?

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.