Skip to content
Merged
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
43b8bcb
doc updated reagrding non equi join
venom1204 Feb 10, 2025
87dcae9
corrected lintr
venom1204 Feb 10, 2025
29e768a
correction
venom1204 Feb 11, 2025
b29de8e
bacticks corrected
venom1204 Feb 14, 2025
dd3b556
correction
venom1204 Feb 14, 2025
4e07ad9
corrected
venom1204 Feb 14, 2025
8fad112
Merge branch 'master' into issue6626
venom1204 Feb 18, 2025
d68b802
Update vignettes/datatable-joins.Rmd
venom1204 Feb 18, 2025
a28a1c8
Merge branch 'master' into issue6626
venom1204 Feb 18, 2025
1245e5e
Merge branch 'master' into issue6626
venom1204 Feb 19, 2025
b556d13
done
venom1204 Feb 19, 2025
8e3ffbe
added explicitly distinguishing
venom1204 Feb 19, 2025
06286b9
Merge branch 'master' into issue6626
venom1204 Feb 20, 2025
6d026bd
removed the temporary part
venom1204 Feb 20, 2025
ed53aca
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
a98f799
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
da0931d
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
008cadf
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
66da346
Merge branch 'master' into issue6626
venom1204 Feb 25, 2025
a6b99b9
updated section
venom1204 Feb 25, 2025
f44211e
more minor style changes
MichaelChirico Feb 25, 2025
fe575ae
revise wording, use consistent capitalizatoin
MichaelChirico Feb 25, 2025
f7ad5e0
revise exposition
MichaelChirico Feb 25, 2025
dcf4178
rm redundant word
MichaelChirico Feb 25, 2025
6357b81
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
41f8c94
Update vignettes/datatable-joins.Rmd
venom1204 Feb 25, 2025
ff7505f
corrected
venom1204 Feb 25, 2025
c0f86bf
Suggest an explicit workaround
MichaelChirico Feb 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions vignettes/datatable-joins.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,54 @@ ProductReceivedProd2[ProductSalesProd2,
nomatch = NULL]
```

### 4.1 Handling Column Name Behavior in Non-Equi Joins

When performing non-equi joins (<, >, <=, >=), it's important to understand how column names are assigned in the result.

- The left operand (`x` column) determines the column name in the result.
- The right operand (`i` column) contributes values but does not retain its original name.
- By default, `data.table` does not retain the `i` column used in the join condition unless explicitly requested.

In non-equi joins, the left side of the operator (e.g., `A` in `A >= B`) must be a column from `x`,
and the right side (e.g., `B`) must be a column from `i`. Non-equi join does not support arbitrary expressions. For example, `on = .(x_col >= i_col)` is valid, but `on = .(x_col >= i_col + 1)` is not.

Arbitrary comparisons can be accomplished by create temporary columns first. For example:

```{r}
x <- data.table(A = 1:5, value_x = letters[1:5])
i <- data.table(B = c(2, 4, 5), value_i = LETTERS[1:3])
x[i, on = .(A >= B)]
```
In data.table, when using a non-equi join condition (>=, <, etc.), the column from x is retained in the result, while the column from i is not retained unless explicitly selected.

Expected Output
```
A value_x value_i
1: 2 b A
2: 4 d B
3: 5 e C
4: 5 e C
```
If multiple rows in x satisfy the join condition with a single row in i, those rows will be duplicated in the result.

If you want to keep the B column from i, you need to explicitly select it in the result:

```{r}
x[i, on = .(A >= B), .(B, A, value_x, value_i)]
```
Updated Output
```
B A value_x value_i
1: 2 2 b A
2: 4 4 d B
3: 5 5 e C
4: 5 5 e C
```
If you want to exclude unmatched rows, you should use nomatch = NULL:

```{r}
x[i, on = .(A >= B), .(B, A, value_x, value_i), nomatch = NULL]
```

## 5. Rolling join

Expand Down
Loading