gpt4 book ai didi

r - 有没有办法在 i 中 self 引用 data.table

转载 作者:行者123 更新时间:2023-12-04 17:07:27 25 4
gpt4 key购买 nike

考虑标准的 data.table 语法 DT[i, j, ...]。由于 .SD 仅在 ji 中的 NULL 中定义,有没有办法隐式(期望)或者显式地(通过类似 .SD 的东西)在 i 中的函数中引用当前的 data.table?

用例

我想编写一个过滤标准列的函数。列名在多个表中是相同的并且有些冗长。为了通过减少输入来加快我的编码速度,我想编写一个这样的函数:

library(data.table)
dt <- data.table(postal_code = c("USA123", "SPEEDO", "USA421"),
customer_name = c("Taylor", "Walker", "Thompson"))
dt
#> postal_code customer_name
#> 1: USA123 Taylor
#> 2: SPEEDO Walker
#> 3: USA421 Thompson

# Filter all customers from a common postal code
# that surname starts with specific letters
extract <- function(x, y, DT) {
DT[, startsWith(postal_code, x) & startsWith(customer_name, y)]
}


# does not work
dt[extract("USA", "T", .SD)]
#> Error in .checkTypos(e, names_x): Object 'postal_code' not found.
#> Perhaps you intended postal_code

# works but requires specifying the data.table explicitly
# plus the drawback that it cannot be called upon, e.g. a grouped .SD
# in a nested call
dt[extract("USA", "T", dt)]
#> postal_code customer_name
#> 1: USA123 Taylor
#> 2: USA421 Thompson

想要的(伪代码)

dt[extract("USA", "T")]
#> postal_code customer_name
#> 1: USA123 Taylor
#> 2: USA421 Thompson

# but also
# subsequent steps in j
dt[extract("USA", "T"), relevant := TRUE][]
#> postal_code customer_name relevant
#> 1: USA123 Taylor TRUE
#> 2: SPEEDO Walker NA
#> 3: USA421 Thompson TRUE

# using other data.tables
another_dt[extract("USA", "T")]
yet_another_dt[extract("USA", "T")]

最佳答案

我不是 data.table 专家,但您可以尝试以下解决方法

> dt[,.SD[extract("USA", "T", .SD)]]
postal_code customer_name
1: USA123 Taylor
2: USA421 Thompson

.SD 中的 j 播放自引用

关于r - 有没有办法在 i 中 self 引用 data.table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70273966/

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