Passing named lists to `.SDcols` / `.SD`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
No source file or test is named. Start by reading the .SD and .SDcols implementation and the related discussions in issues 1063 and 4970; determine the API and evaluation rules for multiple named subsets, then add focused coverage for the proposed grouped aggregation behavior.
Written by the indexing model from the issue text.
Description
Is there any scope/appetite for supporting multiple .SD and .SDcols?
Motivation: I frequently encounter situations where I need to perform different aggregation tasks on distinct column groups. One group of columns will be aggregated as means, another group will be aggregated as medians, yet another group will be aggregated as sums, etc. In these cases, only one group can be passed through the convenience features of .SD(cols), while the other group(s) must all be aggregated manually.
Here's a simple (and somewhat ill-advised) example that illustrates the mechanics:
library(data.table)
d = as.data.table(iris)
d[,
c(lapply(.SD, sum),
list(Petal.Length = mean(Petal.Length), Petal.Width = mean(Petal.Width))),
.SDcols = patterns("^Sepal"),
by = Species]
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1: setosa 250.3 171.4 1.462 0.246
#> 2: versicolor 296.8 138.5 4.260 1.326
#> 3: virginica 329.4 148.7 5.552 2.026
Here the summed Sepal columns get the .SD convenience treatment, while I have to manually take the mean of the Petal columns separately (and name them; see also https://github.com/Rdatatable/data.table/issues/1227#issuecomment-844760088).
My proposal is to allow something like this instead:
d[,
c(lapply(.SD, sum), lapply(.SD2, mean)),
.SDcols = patterns("^Sepal"), .SDcols2 = patterns("^Petal"),
by = Species]
I'm assuming here that you can match the relevant subsets based on the index (.SD2 => .SDcols2). If this is easy to do for one additional subset, then in principle it seems possible for any additional subsets (.SD3 => .SDcols3, etc). Of course, this may impose some small overhead that doesn't pass the cost-benefit test.
Feel free to close if this seems undesirable / too much work. Thanks for considering!
Update. Related: https://github.com/Rdatatable/data.table/issues/1063#issuecomment-748159218 and possibly https://github.com/Rdatatable/data.table/issues/4970
Proposed solution in the former is:
s_cols = grep("^Sepal", names(d), value = TRUE)
p_cols = grep("^Petal", names(d), value = TRUE)
d[,
{
SD = unclass(.SD)
c(lapply(SD[s_cols], sum), lapply(SD[p_cols], mean))
},
.SDcols = c(s_cols, p_cols),
by = Species]
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1: setosa 250.3 171.4 1.462 0.246
#> 2: versicolor 296.8 138.5 4.260 1.326
#> 3: virginica 329.4 148.7 5.552 2.026
SessionInfo
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux
Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblas_haswellp-r0.3.13.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
- 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 ·