使用在data.table中的反引号引用的列名称进行子集化时的奇怪行为

查看使用reprex生成的以下示例:

library(data.table)

DT <- data.table(id = letters[1:3], `counts(a>=0)` = 1:3)

DT[`counts(a>=0)` >= 2]  # 1
#>    id counts(a>=0)
#> 1:  b            2
#> 2:  c            3

DT[`counts(a>=0)` == 2]  # 2
#> Error in `[.data.table`(DT, `counts(a>=0)` == 2): Column(s) [counts(a] not found in x

DT[id == "a"]  # 3
#>    id counts(a>=0)
#> 1:  a            1

由于标有#1和#3的行都有效,我想知道为什么用`count(a> = 0)`== 2(#2)进行子集化不起作用.

SessionInfo:

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

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] reprex_0.1.2      data.table_1.11.2

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     rprojroot_1.3-2  digest_0.6.15    crayon_1.3.4     withr_2.1.2      assertthat_0.2.0 R6_2.2.2        
 [8] backports_1.1.2  magrittr_1.5     formatR_1.5      evaluate_0.10.1  stringi_1.1.6    debugme_1.1.0    rstudioapi_0.7  
[15] callr_2.0.2      whisker_0.3-2    rmarkdown_1.9    devtools_1.13.5  tools_3.4.4      stringr_1.3.0    yaml_2.1.17     
[22] compiler_3.4.4   htmltools_0.3.6  memoise_1.1.0    knitr_1.20    

最佳答案 它对我有用:

DT[as.numeric(`counts(a>=0)`) == 2]
点赞