gpt4 book ai didi

r - 即使不需要,如何在 R 的 quote() 中保留 `backticks`?

转载 作者:行者123 更新时间:2023-12-04 10:40:31 24 4
gpt4 key购买 nike

知道一些字符如- (连字符)或 (space) 不应在常规 R 名称中使用,我需要在特定场景中使用它们。

以下是用于说明问题的最小可重现示例。

鉴于下表

library(data.table)

dat <- data.table(x = 1:8,
y = rep(rep(c("a", "b"), each = 2), 2),
`y-z` = rep(c("c", "d"), each = 4))

我能够做到以下几点。
dat[, sum(x), by = y]     # works
dat[, sum(x), by = `y`] # works
dat[, sum(x), by = `y-z`] # works

dat[, sum(x), by = eval("`y`, `y-z`", envir = .SD)] # works

现在考虑我想在列表或普通(无所谓)中指定一些查询。
arg1 <- quote(`y`)
arg2 <- quote(`y-z`)

arg_list <- list(arg1 = quote(`y`),
arg2 = quote(`y-z`),
arg3 = quote(`y` == "a"),
arg4 = quote(`y-z` == "d"))

仅当我的论点需要反引号并且没有在 quote() 中预先解析/反解析时,我才能执行以下操作.
# simple cases
dat[, sum(x), by = eval(arg1, envir = .SD)] # works
dat[, sum(x), by = eval(arg2, envir = .SD)] # works
dat[, sum(x), by = eval(arg_list[["arg1"]], envir = .SD)] # works
dat[, sum(x), by = eval(arg_list[["arg2"]], envir = .SD)] # works

# complex cases in which "by" is constructed
dat[, sum(x), by = .(`y`, `y-z`)] # works
dat[, sum(x), by = eval("`y`, `y-z`", envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", "`y-z`"), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg2), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", "`y`"), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg1), collapse = ""), envir = .SD)] # fails

dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg_list[["arg2"]]), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg_list[["arg1"]]), collapse = ""), envir = .SD)] # fails

# probably even more complex cases
dat[, sum(x), by = eval(paste(c("`y`, sub = `y-z` == 'd'"), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg_list[["arg4"]]), collapse = ""), envir = .SD)] # works
dat[, sum(x), by = eval(paste(c("`y`,", "sub =", arg_list[["arg3"]]), collapse = ""), envir = .SD)] # fails

当我检查 arg1 的输出时,我知道反引号已经消失了。和 arg_list[["arg1"]]arg_list[["arg4"]] .但显然,我需要它们在 data.table 中构建正确的查询。的 by =争论。

有没有办法让所有情况都解决?谢谢!

最佳答案

您可以 eval最后,这可能会更简单。例如,

> ex = substitute(dat[, sum(x), by=myby], list(myby = quote(list(y, `y-z`))))
> eval(ex)
y y-z V1
1: a c 3
2: b c 7
3: a d 11
4: b d 15

那么你的问题是如何构建表达式 quote(list(y, `y-z`)) ,这是一个基本的 R 问题,不需要详细了解什么 DT[, ..., by=eval(...)]在内部做。

对于 OP 的对象,这是一种方法,使用 call :
myby = with(arg_list, call("list", arg1, arg2))
ex = substitute(dat[, sum(x), by=myby], list(myby = myby) )
eval(ex)

从 OP 的评论中,您还可以在 call(...) 中命名参数。 :

In case someone is looking for named arguments in data.table, one can simply use an unquoted expression myby = with(arg_list, call("list", arg1, sub = arg3)). – ninjaminb

关于r - 即使不需要,如何在 R 的 quote() 中保留 `backticks`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59955924/

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