列选择取决于J表达式吗?

时间:2019-10-28 12:05:57

标签: r data.table

对于感兴趣的人,我在github上开设了一个期刊。


请考虑以下两个示例:

> 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"

选项1和选项2中的J表示形式相似,但结果不同。我知道我可以通过以下方式做到这一点:

> # option 3
> x <- grep('Length', names(iris), value = TRUE)
> iris[, c('Species', ..x)]
       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

但是,我不知道为什么在将选项2评估为字符向量时,选项1会导致选择列。

> 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   

0 个答案:

没有答案
相关问题