number of columns returned varies dependant on column name in j when .SDcols has duplicates

Open
#3,625 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by running the supplied iris reprex with the current data.table version and compare each .SDcols case. Trace how duplicate names are handled during .SD and j evaluation. Done means the intended column-count behavior is established and the reproduced cases are covered by a regression test.

Written by the indexing model from the issue text.

Description

consistency

Hi All,
Thank you for a fantastic package.
I recently discovered an inconsistent behaviour when passing a vector of non-unique columns into .SDcols.
When a column of DT is referenced in j and .SDcols contains a non-unique vector of column names, the number of columns of the result varies depending on whether the column referenced in j is contained in .SDcols.

Please see the reprex below.

DT <- as.data.table(iris)
DT

# Non-unique columns to be passed into .SDcols
cols <- c("Sepal.Length", "Sepal.Length", "Sepal.Width")

# .SDcols recognizes and carries through non-unique columns:
ncol(
    DT[, .SD, .SDcols = cols]
    )

# Reference a column *not* already in .SDcols and only the unique columns are returned
ncol(
    DT[, cbind(.SD, Petal.Width), .SDcols = cols]
    )

# Reference a column already in .SDcols and the precise .SDcols columns are returned
ncol(
    DT[, cbind(.SD, Sepal.Width), .SDcols = cols]
    )

# functions on these columns also carry-through three columns
ncol(
    DT[, lapply(.SD, sum), .SDcols = cols]
)

# functions on these columns that also reference one of .SDcols carry-through three columns
ncol(
    DT[, lapply(.SD, function(x){
        x + Sepal.Length
    }), .SDcols = cols]
)

ncol(
    DT[, lapply(.SD, function(x){
        x + Sepal.Width
    }), .SDcols = cols]
)

# functions on these columns that reference a column, not contained in .SDcols does *not* carry-through three columns
ncol(
    DT[, lapply(.SD, function(x){
        x + Petal.Length
    }), .SDcols = cols]
)

And here is the sessionInfo output:

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.6 (Maipo)

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8      
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] graphics  grDevices datasets  stats     utils     methods   base     

other attached packages:
[1] sparklyr_0.8.5.2         readr_1.1.1              dplyr_0.7.8              plyr_1.8.4               ggplot2_3.1.0            AdvancedAnalytics_0.41.7 xgboost_0.4-4           
[8] data.table_1.11.8       

loaded via a namespace (and not attached):
 [1] httr_1.4.0         tidyr_0.8.2        jsonlite_1.6       splines_3.4.3      foreach_1.4.4      shiny_1.0.5        assertthat_0.2.0   stats4_3.4.3       yaml_2.2.0         pillar_1.3.1      
[11] backports_1.1.2    lattice_0.20-35    quantreg_5.33      glue_1.3.0         digest_0.6.18      RColorBrewer_1.1-2 promises_1.0.1     checkmate_1.8.5    minqa_1.2.4        colorspace_1.4-0  
[21] htmltools_0.3.6    httpuv_1.4.5       Matrix_1.2-12      pkgconfig_2.0.2    broom_0.5.0        SparseM_1.77       caret_6.0-76       purrr_0.2.4        xtable_1.8-3       scales_1.0.0      
[31] later_0.7.4        lme4_1.1-18-1      MatrixModels_0.4-1 tibble_2.0.1       mgcv_1.8-22        car_2.1-5          withr_2.1.2        nnet_7.3-12        lazyeval_0.2.1     pbkrtest_0.4-7    
[41] fst_0.8.2          magrittr_1.5       crayon_1.3.4       mime_0.6           lightgbm_2.1.0     doParallel_1.0.14  nlme_3.1-131       MASS_7.3-47        FNN_1.1            tools_3.4.3       
[51] hash_2.2.6         hms_0.4.2          stringr_1.3.1      munsell_0.5.0      bindrcpp_0.2.2     compiler_3.4.3     rlang_0.3.1        aaDevOps_0.2.3     grid_3.4.3         nloptr_1.2.1      
[61] iterators_1.0.10   rstudioapi_0.9.0   base64enc_0.1-3    gtable_0.2.0       ModelMetrics_1.1.0 codetools_0.2-15   DBI_1.0.0          reshape2_1.4.3     R6_2.4.0           ROracle_1.3-1     
[71] gridExtra_2.3      lubridate_1.7.4    RJDBC_0.2-5        bindr_0.1.1        rprojroot_1.3-2    rJava_0.9-10       stringi_1.2.4      parallel_3.4.3     Rcpp_1.0.0         dbplyr_1.2.2      
[81] tidyselect_0.2.5  

Thank you very much,
Michael Hirsch

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.