如何在data.table中以编程方式选择列?

时间:2022-08-08 09:22:14

I have the following data.table (DT):

我有以下data.table(DT):

DT <- data.table(V1 = 1:3, V2 = 4:6, V3 = 7:9)

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”中的两列“V1”和“V3”

keep <- c("V1", "V3")

If we were to select the "keep" columns from a data.frame, the following would work:

如果我们从data.frame中选择“保留”列,则以下内容将起作用:

DT[keep]

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?

不幸的是,当这是data.table时,这不起作用。我认为data.frame和data.table与这种行为相同,但显然它们不是。有人能提供正确的语法建议吗?

1 个解决方案

#1


15  

This is covered in FAQ 1.1, 1.2 and 2.17.

这包含在FAQ 1.1,1.2和2.17中。

Some possibilities:

一些可能性:

DT[, keep, with = FALSE]
DT[, c('V1', 'V3'), with = FALSE]
DT[, c(1, 3), with = FALSE]
DT[, list(V1, V3)]

The reason DF[c('V1','V3')] works as it does for a data.frame is covered in ?`[.data.frame`

DF [c('V1','V3')]的工作原理与data.frame的工作原理有关.` [。data.frame`

Data frames can be indexed in several modes. When [ and [[ are used with a single vector index (x[i] or x[[i]]), they index the data frame as if it were a list. In this usage a drop argument is ignored, with a warning.

数据帧可以以多种模式索引。当[和[[与单个向量索引(x [i]或x [[i]]一起使用)时,它们将数据帧索引,就好像它是一个列表一样。在此用法中,将忽略drop参数,并显示警告。


From data.table 1.10.2, you may use the .. prefix when subsetting columns programmatically:

从data.table 1.10.2开始,您可以在以编程方式对列进行子集化时使用..前缀:

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.

当j是带有前缀的符号时,它将在调用范围中查找,其值被视为列名或数字[...]它是实验性的。

Thus:

从而:

DT[ , ..keep]
#    V1 V3
# 1:  1  7
# 2:  2  8
# 3:  3  9

#1


15  

This is covered in FAQ 1.1, 1.2 and 2.17.

这包含在FAQ 1.1,1.2和2.17中。

Some possibilities:

一些可能性:

DT[, keep, with = FALSE]
DT[, c('V1', 'V3'), with = FALSE]
DT[, c(1, 3), with = FALSE]
DT[, list(V1, V3)]

The reason DF[c('V1','V3')] works as it does for a data.frame is covered in ?`[.data.frame`

DF [c('V1','V3')]的工作原理与data.frame的工作原理有关.` [。data.frame`

Data frames can be indexed in several modes. When [ and [[ are used with a single vector index (x[i] or x[[i]]), they index the data frame as if it were a list. In this usage a drop argument is ignored, with a warning.

数据帧可以以多种模式索引。当[和[[与单个向量索引(x [i]或x [[i]]一起使用)时,它们将数据帧索引,就好像它是一个列表一样。在此用法中,将忽略drop参数,并显示警告。


From data.table 1.10.2, you may use the .. prefix when subsetting columns programmatically:

从data.table 1.10.2开始,您可以在以编程方式对列进行子集化时使用..前缀:

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.

当j是带有前缀的符号时,它将在调用范围中查找,其值被视为列名或数字[...]它是实验性的。

Thus:

从而:

DT[ , ..keep]
#    V1 V3
# 1:  1  7
# 2:  2  8
# 3:  3  9