gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:39:52 27 4
gpt4 key购买 nike

看下面用 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 工作,我想知道为什么用 `counts(a>=0)` == 2 子集( #2 ) 不起作用。

session 信息:
> 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]

关于r - 使用 data.table 的 I 中用反引号引用的列名进行子集化时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50810759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com