@@ -703,76 +703,84 @@ Products[!"popcorn",
703703Use ` := ` to modify columns ** by reference** (no copy) during joins. General syntax: ` x[i, on=, (cols) := val] ` .
704704
705705** Simple One-to-One Update**
706+
706707Update ` Products ` with prices from ` ProductPriceHistory ` :
707708
708709``` {r}
709710Products[ProductPriceHistory,
710711 on = .(id = product_id),
711712 price := i.price]
712713```
714+
713715- ` i.price ` refers to price from ` ProductPriceHistory ` .
714716- Modifies ` Products ` in-place.
715717
716718** Grouped Updates with ` .EACHI ` **
719+
717720Get last price/date for each product:
721+
718722``` {r Updating_with_the_Latest_Record}
719723Products[ProductPriceHistory,
720724 on = .(id = product_id),
721725 `:=`(price = last(i.price), last_updated = last(i.date)),
722726 by = .EACHI]
723727```
728+
724729- ` by = .EACHI ` groups by i's rows (1 group per ProductPriceHistory row).
725730- ` last() ` returns last value
726731
727732** Efficient Right Join Update**
733+
728734Add product details to ` ProductPriceHistory ` without copying:
729735
730736``` {r}
731737cols <- setdiff(names(Products), "id")
732738ProductPriceHistory[, (cols) :=
733739 Products[.SD, on = .(id = product_id), .SD, .SDcols = cols]]
734740```
741+
735742- In ` i ` , ` .SD ` refers to ` ProductPriceHistory ` .
736743- In ` j ` , ` .SD ` refers to ` Products ` .
737744- Updates ` ProductPriceHistory ` by reference.
738745
739746** Handling Edge Cases and Dynamic Column Updates**
747+
740748To dynamically update columns and handle missing values:
749+
741750``` {r}
742751cols <- setdiff(names(Products), "id")
743752ProductPriceHistory[, (cols) :=
744753 Products[.SD, on = .(id = product_id), .SD, .SDcols = cols]]
745754setnafill(ProductPriceHistory, fill=0, cols="price") # Handle missing values
746755```
756+
747757- Ensures unmatched values do not propagate ` NA ` unintentionally.
748758
749759** Dynamic Column Selection and Updates**
750760Columns can be dynamically updated based on variable names:
761+
751762``` {r}
752763my_var_name <- "price"
753764Products[ProductPriceHistory, on = .(id = product_id),
754765 (my_var_name) := i.price]
755766```
767+
756768- This approach allows flexibility in specifying columns programmatically.
757769
758770** Iterating Through Multiple Columns for Updates**
771+
759772Dynamically updating multiple columns from ` ProductPriceHistory ` :
773+
760774``` {r}
761775update_cols <- intersect(c("price", "category", "stock"), names(ProductPriceHistory))
762- ```
763776for (col in update_cols) {
764777 Products[ProductPriceHistory,
765778 on = .(id = product_id),
766779 (col) := i[[col]],
767780 env = list(col = col)]}
768781```
769- - Ensures multiple columns are updated efficiently in a loop.
770782
771- **Summary**
772- - `last(x)` vs `tail(x,1)`: Both return last element, but `tail()` returns list for lists.
773- - `:=` always modifies `x`, never `i`. For right joins, update `i` directly via `i[, ... := x[.SD]]`.
774- - `.EACHI` is crucial for per-row operations; simple joins use first match.
775- ***
783+ - Ensures multiple columns are updated efficiently in a loop.
776784
777785## Reference
778786
0 commit comments