Using .SD with update by reference
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·