Using .SD with update by reference

Open
#6,997 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the supplied Products and ProductPriceHistory example in R, then inspect the [.data.table update-by-reference and .SDcols path implicated by the error. Determine whether the requested .SD form requires a behavior change or documentation, add coverage for the chosen outcome, and verify the existing join example remains unchanged.

Written by the indexing model from the issue text.

Description

joins question

First, let me introduce the current example in https://rdatatable.gitlab.io/data.table/articles/datatable-joins.html#updating-by-reference

library(data.table)
Products = data.table(
  id = c(1:4,
         NA_integer_),
  name = c("banana",
           "carrots",
           "popcorn",
           "soda",
           "toothpaste"),
  price = c(0.63,
            0.89,
            2.99,
            1.49,
            2.99),
  unit = c("unit",
           "lb",
           "unit",
           "ounce",
           "unit"),
  type = c(rep("natural", 2L),
           rep("processed", 3L))
)

Products
#       id       name price   unit      type
#    <int>     <char> <num> <char>    <char>
# 1:     1     banana  0.63   unit   natural
# 2:     2    carrots  0.89     lb   natural
# 3:     3    popcorn  2.99   unit processed
# 4:     4       soda  1.49  ounce processed
# 5:    NA toothpaste  2.99   unit processed

ProductPriceHistory = data.table(
  product_id = rep(1:2, each = 3),
  date = rep(as.IDate(c("2024-01-01", "2024-02-01", "2024-03-01")), 2),
  price = c(0.59, 0.63, 0.65,  # Banana prices
            0.79, 0.89, 0.99)  # Carrot prices
)

ProductPriceHistory
#    product_id       date price
#         <int>     <IDat> <num>
# 1:          1 2024-01-01  0.59
# 2:          1 2024-02-01  0.63
# 3:          1 2024-03-01  0.65
# 4:          2 2024-01-01  0.79
# 5:          2 2024-02-01  0.89
# 6:          2 2024-03-01  0.99

copy(Products)[ProductPriceHistory,
               on = .(id = product_id),
               j = `:=`(price = tail(i.price, 1),
                        last_updated = tail(i.date, 1)),
               by = .EACHI][]
#       id       name price   unit      type last_updated
#    <int>     <char> <num> <char>    <char>       <IDat>
# 1:     1     banana  0.65   unit   natural   2024-03-01
# 2:     2    carrots  0.99     lb   natural   2024-03-01
# 3:     3    popcorn  2.99   unit processed         <NA>
# 4:     4       soda  1.49  ounce processed         <NA>
# 5:    NA toothpaste  2.99   unit processed         <NA>

I want to do this, but with price and date inside .SD, but I'm getting errors:

Products[ProductPriceHistory, on = .(id = product_id), c("price", "last_updated") := lapply(.SD, tail, 1), .SDcols = c("i.price", "i.date"), by = .EACHI]
Error in `[.data.table`(Products, ProductPriceHistory, on = .(id = product_id),  : 
  Some items of .SDcols are not column names: [i.price, i.date]

I tried other possibilities (removing i. of column names in .SDcols, using i.SD, i..SD...), with similar results.

How may I proceed?

Thanks!

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.