Skip to content

Commit 29062d5

Browse files
committed
updated section
1 parent d39fc1c commit 29062d5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

vignettes/datatable-joins.Rmd

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ Products[ProductPriceHistory,
721721
`:=`(price = last(i.price), last_updated = last(i.date)),
722722
by = .EACHI]
723723
```
724-
- `by = .EACHI` groups by i's rows (1 group per Products row).
724+
- `by = .EACHI` groups by i's rows (1 group per ProductPriceHistory row).
725725
- `last()` returns last value including `NA`:
726726

727727
```{r}
@@ -762,16 +762,20 @@ Dynamically updating multiple columns from `ProductPriceHistory`:
762762
```{r}
763763
update_cols <- intersect(c("price", "category", "stock"), names(ProductPriceHistory))
764764
765+
```
765766
for (col in update_cols) {
766-
Products[ProductPriceHistory, on = .(id = product_id), (col) := get(paste0("i.", col))]}
767+
Products[ProductPriceHistory,
768+
on = .(id = product_id),
769+
(col) := i[[col]],
770+
env = list(col = col)]}
767771
```
768772
- Ensures multiple columns are updated efficiently in a loop.
769773
770774
**Summary**
771775
- `last(x)` vs `tail(x,1)`: Both return last element, but `tail()` returns list for lists.
772776
- `:=` always modifies `x`, never `i`. For right joins, update `i` directly via `i[, ... := x[.SD]]`.
773777
- `.EACHI` is crucial for per-row operations; simple joins use first match.
774-
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.
775779
***
776780
777781
## Reference

0 commit comments

Comments
 (0)