Skip to content

should support DT[FLAG, ] when FLAG is a logical vector of DT #4255

@shrektan

Description

@shrektan

If DT contains a logical vector (call it the FLAG column), we have to use DT[FLAG==TRUE] while DT[FLAG] will raise errors. It's kind of confusing for the newbies.

Any reason that we can't support this?

library(data.table)
tbl <- data.table(FLAG = c(TRUE, FALSE), VALUE = c(1, 0))
tbl[FLAG == TRUE]
#>    FLAG VALUE
#> 1: TRUE     1
tbl[FLAG == FALSE]
#>     FLAG VALUE
#> 1: FALSE     0
tbl[FLAG]
#> Error in `[.data.table`(tbl, FLAG): FLAG is not found in calling scope but it is a column of type logical. If you wish to select rows where that column contains TRUE, or perhaps that column contains row numbers of itself to select, try DT[(col)], DT[DT$col], or DT[col==TRUE] is particularly clear and is optimized. When the first argument inside DT[...] is a single symbol (e.g. DT[var]), data.table looks for var in calling scope.
tbl[!FLAG]
#> Error in `[.data.table`(tbl, !FLAG): FLAG is not found in calling scope but it is a column of type logical. If you wish to select rows where that column contains TRUE, or perhaps that column contains row numbers of itself to select, try DT[(col)], DT[DT$col], or DT[col==TRUE] is particularly clear and is optimized. When the first argument inside DT[...] is a single symbol (e.g. DT[var]), data.table looks for var in calling scope.
flag <- tbl$FLAG
tbl[flag]
#>    FLAG VALUE
#> 1: TRUE     1
tbl[!flag]
#>     FLAG VALUE
#> 1: FALSE     0

Created on 2020-02-23 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       macOS Catalina 10.15.3      
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Asia/Shanghai               
#>  date     2020-02-23                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                    
#>  backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.0)                    
#>  callr         3.4.1      2020-01-24 [1] CRAN (R 3.6.0)                    
#>  cli           2.0.1      2020-01-08 [1] CRAN (R 3.6.0)                    
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                    
#>  data.table  * 1.12.8     2019-12-09 [1] CRAN (R 3.6.0)                    
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)                    
#>  devtools      2.2.1      2019-09-24 [1] CRAN (R 3.6.0)                    
#>  digest        0.6.23     2019-11-23 [1] CRAN (R 3.6.0)                    
#>  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.0)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                    
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.0)                    
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                    
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                    
#>  highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)                    
#>  htmltools     0.4.0.9002 2020-01-27 [1] Github (rstudio/htmltools@e07546c)
#>  knitr         1.27.2     2020-01-31 [1] local                             
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                    
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)                    
#>  pkgbuild      1.0.6      2019-10-09 [1] CRAN (R 3.6.0)                    
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)                    
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 3.6.0)                    
#>  processx      3.4.1      2019-07-18 [1] CRAN (R 3.6.0)                    
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.6.0)                    
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                    
#>  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.0)                    
#>  remotes       2.1.0      2019-06-24 [1] CRAN (R 3.6.0)                    
#>  rlang         0.4.4      2020-01-28 [1] CRAN (R 3.6.2)                    
#>  rmarkdown     2.1        2020-01-20 [1] CRAN (R 3.6.0)                    
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                    
#>  stringi       1.4.5      2020-01-11 [1] CRAN (R 3.6.0)                    
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                    
#>  testthat      2.3.1      2019-12-01 [1] CRAN (R 3.6.0)                    
#>  usethis       1.5.1      2019-07-04 [1] CRAN (R 3.6.0)                    
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                    
#>  xfun          0.12       2020-01-13 [1] CRAN (R 3.6.0)                    
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 3.6.2)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions