如何以编程方式在 data.table 中选择列? [英] How to select columns programmatically in a data.table?
问题描述
我有以下 data.table (DT):
I have the following data.table (DT):
DT <- data.table(V1 = 1:3, V2 = 4:6, V3 = 7:9)
我想通过使用存储相关变量名称的对象以编程方式(动态)选择变量的子集.例如,我想选择存储在变量keep"中的V1"和V3"两列
I would like to select a subset of the variables programmatically (dynamically), by using an object where the relevant variable names are stored. For example, I want to select the two columns "V1" and "V3" stored in a variable "keep"
keep <- c("V1", "V3")
如果我们要从 data.frame 中选择保留"列,则可以使用以下方法:
If we were to select the "keep" columns from a data.frame, the following would work:
DT[keep]
不幸的是,当这是一个 data.table 时,这不起作用.我认为 data.frame 和 data.table 与这种行为相同,但显然它们不是.有人能就正确的语法提出建议吗?
Unfortunately, this is not working when this is a data.table. I thought the data.frame and data.table are identical with this kind of behavior, but apperently they aren't. Anybody able to advise on the correct syntax?
推荐答案
一些可能性:
DT[, keep, with = FALSE]
DT[, c('V1', 'V3'), with = FALSE]
DT[, c(1, 3), with = FALSE]
DT[, list(V1, V3)]
DF[c('V1','V3')]
像 data.frame
一样工作的原因在 ?`[.data.frame`
The reason DF[c('V1','V3')]
works as it does for a data.frame
is covered in ?`[.data.frame`
可以在多种模式下对数据帧进行索引.使用 [
和 [[
时使用单个向量索引(x[i]
或 x[[i]]
),它们索引数据帧好像它是一个列表.在这种用法中,一个 drop
参数被忽略,带有一个警告.
Data frames can be indexed in several modes. When
[
and[[
are used with a single vector index (x[i]
orx[[i]]
), they index the data frame as if it were a list. In this usage adrop
argument is ignored, with a warning.
<小时>
来自 data.table 1.10.2
,您可以在以编程方式设置列子集时使用 ..
前缀:
当 j
是一个以 ..
为前缀的符号时,它将在调用范围内查找,其值被视为列名或数字 [...]是实验性的.
When
j
is a symbol prefixed with..
it will be looked up in calling scope and its value taken to be column names or numbers [...] It is experimental.
因此:
DT[ , ..keep]
# V1 V3
# 1: 1 7
# 2: 2 8
# 3: 3 9
这篇关于如何以编程方式在 data.table 中选择列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!