Column selection without `with = FALSE` is dependent on J expression

Open
#4,004 2 comments 2 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
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the two iris data.table expressions from the issue and trace how the j expression handles c, paste0, and grep. Done means the column-selection behavior is consistent with the documented syntax, with regression coverage for both examples and the intended with = FALSE or parenthesized alternatives.

Written by the indexing model from the issue text.

Description

consistency feature request programming

(This question is asked on SO with some helpful comments)

Consider the following two examples. The J expresssion is similar in option 1 and option 2, but the results are different: option 1 results in column selection while option 2 is evaluated into a character vector.

> library(data.table)
> iris <- as.data.table(iris)

> # option 1
> iris[, c('Species', paste0(c('Sepal.', 'Petal.'), 'Length'))]
       Species Sepal.Length Petal.Length
  1:    setosa          5.1          1.4
  2:    setosa          4.9          1.4
  3:    setosa          4.7          1.3
  4:    setosa          4.6          1.5
  5:    setosa          5.0          1.4
 ---                                    
146: virginica          6.7          5.2
147: virginica          6.3          5.0
148: virginica          6.5          5.2
149: virginica          6.2          5.4
150: virginica          5.9          5.1

> # option 2
> iris[, c('Species', grep('Length', names(iris), value = TRUE))] 
[1] "Species"      "Sepal.Length" "Petal.Length"

In the comment of the SO post, David Arenburg found that "functions such paste and c are being explicitly searched in the j expression and then set with=FALSE, similar to what happens with .. prefix. grep isn't being searched". From a user's perspective, I think this is very unconvenient. Rather than gusssing what users want to do, it is easier to explicit require users specifiy their needs using with = FALSE or other syntax such as (c(...)) as commented by G. Grothendieck on the SO post.

Here is the sessionInfo:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               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    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] data.table_1.12.2

loaded via a namespace (and not attached):
[1] compiler_3.6.1 tools_3.6.1   
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.