Skip to content

Commit ff365ac

Browse files
committed
included examples
1 parent a6e4be1 commit ff365ac

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

vignettes/datatable-joins.Rmd

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ Products[!"popcorn",
702702

703703
Use `:=` to modify columns **by reference** (no copy) during joins. General syntax: `x[i, on=, (cols) := val]`.
704704

705-
- Simple One-to-One Update
705+
**Simple One-to-One Update**
706706
Update `Products` with prices from `ProductPriceHistory`:
707707

708708
```{r}
@@ -713,7 +713,7 @@ Products[ProductPriceHistory,
713713
- i.price refers to price from i (ProductPriceHistory).
714714
- Modifies Products in-place.
715715

716-
- Grouped Updates with `.EACHI`
716+
**Grouped Updates with `.EACHI`**
717717
Get last price/date for each product:
718718
```{r Updating_with_the_Latest_Record}
719719
Products[ProductPriceHistory,
@@ -727,7 +727,7 @@ Products[ProductPriceHistory,
727727
```{r}
728728
data.table::last(c(1, NA)) # NA
729729
```
730-
- Efficient Right Join Update
730+
**Efficient Right Join Update**
731731
Add product details to ProductPriceHistory without copying:
732732

733733
```{r}
@@ -738,9 +738,39 @@ ProductPriceHistory[, (cols) :=
738738
- .SD refers to ProductPriceHistory during the join.
739739
- Updates ProductPriceHistory by reference.
740740

741-
- last(x) vs tail(x,1): Both return last element, but tail() returns list for lists.
742-
- := always modifies x, never i. For right joins, update i directly via i[, ... := x[.SD]].
743-
- .EACHI is crucial for per-row operations; simple joins use first match.
741+
**Handling Edge Cases and Dynamic Column Updates**
742+
To dynamically update columns and handle missing values:
743+
```{r}
744+
cols <- setdiff(names(Products), "id")
745+
ProductPriceHistory[, (cols) :=
746+
Products[.SD, on = .(id = product_id), .SD, .SDcols = cols]]
747+
ProductPriceHistory[is.na(price), price := 0] # Handle missing values
748+
```
749+
- Ensures unmatched values do not propagate `NA` unintentionally.
750+
751+
**Dynamic Column Selection and Updates**
752+
Columns can be dynamically updated based on variable names:
753+
```{r}
754+
my_var_name <- "price"
755+
Products[ProductPriceHistory, on = .(id = product_id),
756+
(my_var_name) := i.price]
757+
```
758+
- This approach allows flexibility in specifying columns programmatically.
759+
760+
**Iterating Through Multiple Columns for Updates**
761+
Dynamically updating multiple columns from `ProductPriceHistory`:
762+
```{r}
763+
update_cols <- intersect(c("price", "category", "stock"), names(ProductPriceHistory))
764+
765+
for (col in update_cols) {
766+
Products[ProductPriceHistory, on = .(id = product_id), (col) := get(paste0("i.", col))]}
767+
```
768+
- Ensures multiple columns are updated efficiently in a loop.
769+
770+
**Summary**
771+
- `last(x)` vs `tail(x,1)`: Both return last element, but `tail()` returns list for lists.
772+
- `:=` always modifies `x`, never `i`. For right joins, update `i` directly via `i[, ... := x[.SD]]`.
773+
- `.EACHI` is crucial for per-row operations; simple joins use first match.
744774

745775
***
746776

0 commit comments

Comments
 (0)