更新时间:2023-11-07 18:55:52
这里是我要做的:
## Create an example of a data.table "dt" whose columns you want to index
## using a character vector "xx"
library(data.table)
dt <- data.table(mtcars)
xx <- c("wt", "mpg")
## Construct a call object identical to that produced by quote(list("wt", "mpg"))
jj <- as.call(lapply(c("list", xx), as.symbol))
## Try it out
dt[1:5,eval(jj)]
# wt mpg
# 1: 2.620 21.0
# 2: 2.875 21.0
# 3: 2.320 22.8
# 4: 3.215 21.4
# 5: 3.440 18.7
计算语言,这样常常有助于看看结构你试图构造的对象。基于以下内容(一旦你知道 as.call()
和 as.symbol()
),所需的语言对象变成一块蛋糕:
When "computing on the language" like this, it's often helpful to have a look at the structure of the object you're trying to construct. Based on the following (and once you know about as.call()
and as.symbol()
), creating the desired language object becomes a piece of cake:
x <- quote(list(wt, mpg))
str(x)
# language list(wt, mpg)
class(x)
# [1] "call"
str(as.list(x))
# List of 3
# $ : symbol list
# $ : symbol wt
# $ : symbol mpg