Skip to content

Commit 283f21c

Browse files
Various suggested improvements
1 parent 29062d5 commit 283f21c

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

vignettes/datatable-joins.Rmd

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ Products[ProductPriceHistory,
710710
on = .(id = product_id),
711711
price := i.price]
712712
```
713-
- `i.price` refers to price from `i` `(ProductPriceHistory)`.
714-
- Modifies Products in-place.
713+
- `i.price` refers to price from `ProductPriceHistory`.
714+
- Modifies `Products` in-place.
715715

716716
**Grouped Updates with `.EACHI`**
717717
Get last price/date for each product:
@@ -722,11 +722,8 @@ Products[ProductPriceHistory,
722722
by = .EACHI]
723723
```
724724
- `by = .EACHI` groups by i's rows (1 group per ProductPriceHistory row).
725-
- `last()` returns last value including `NA`:
725+
- `last()` returns last value
726726

727-
```{r}
728-
data.table::last(c(1, NA)) # NA
729-
```
730727
**Efficient Right Join Update**
731728
Add product details to `ProductPriceHistory` without copying:
732729

@@ -735,7 +732,8 @@ cols <- setdiff(names(Products), "id")
735732
ProductPriceHistory[, (cols) :=
736733
Products[.SD, on = .(id = product_id), .SD, .SDcols = cols]]
737734
```
738-
- `.SD` refers to `ProductPriceHistory` during the join.
735+
- In `i`, `.SD` refers to `ProductPriceHistory`.
736+
- In `j`, `.SD` refers to `Products`.
739737
- Updates `ProductPriceHistory` by reference.
740738

741739
**Handling Edge Cases and Dynamic Column Updates**
@@ -744,7 +742,7 @@ To dynamically update columns and handle missing values:
744742
cols <- setdiff(names(Products), "id")
745743
ProductPriceHistory[, (cols) :=
746744
Products[.SD, on = .(id = product_id), .SD, .SDcols = cols]]
747-
ProductPriceHistory[is.na(price), price := 0] # Handle missing values
745+
setnafill(ProductPriceHistory, fill=0, cols="price") # Handle missing values
748746
```
749747
- Ensures unmatched values do not propagate `NA` unintentionally.
750748

@@ -761,7 +759,6 @@ Products[ProductPriceHistory, on = .(id = product_id),
761759
Dynamically updating multiple columns from `ProductPriceHistory`:
762760
```{r}
763761
update_cols <- intersect(c("price", "category", "stock"), names(ProductPriceHistory))
764-
765762
```
766763
for (col in update_cols) {
767764
Products[ProductPriceHistory,
@@ -775,7 +772,6 @@ for (col in update_cols) {
775772
- `last(x)` vs `tail(x,1)`: Both return last element, but `tail()` returns list for lists.
776773
- `:=` always modifies `x`, never `i`. For right joins, update `i` directly via `i[, ... := x[.SD]]`.
777774
- `.EACHI` is crucial for per-row operations; simple joins use first match.
778-
- Note: Older functions like `mapvalues()` from the deprecated `plyr` package were previously used for recoding values. It is recommended to use data.table’s native update-join methods for efficient and future-proof code.
779775
***
780776
781777
## Reference

0 commit comments

Comments
 (0)