在 data.table 中选择列的子集 [英] Selecting a subset of columns in a data.table
问题描述
我想打印数据表 dt
的所有列,除了其中一个名为 V3
但不想按数字引用它,而是按名称.这是我的代码:
I'd like to print all the columns of a data table dt
except one of them named V3
but don't want to refer to it by number but by name. This is the code that I have:
dt = data.table(matrix(sample(c(0,1),5,rep=T),50,10))
dt[,-3,with=FALSE] # Is this the only way to not print column "V3"?
使用data frame
的方式,可以通过代码做到这一点:
Using the data frame
way, one could do this through the code:
df = data.frame(matrix(sample(c(0,1),5,rep=T),50,10))
df[,!(colnames(df)%in% c("X3"))]
所以,我的问题是:是否有另一种方法可以不打印数据表中的一列而无需按数字引用它?我想找到类似于我上面使用的数据框语法但使用数据表的东西.
So, my question is: is there another way to not print one column in a data table without the necessity of refer to it by number? I'd like to find something similar to the data frame syntax I used above but using data table.
推荐答案
使用与 data.frame
非常相似的语法,但添加参数 with=FALSE
:
Use a very similar syntax as for a data.frame
, but add the argument with=FALSE
:
dt[, setdiff(colnames(dt),"V9"), with=FALSE]
V1 V2 V3 V4 V5 V6 V7 V8 V10
1: 1 1 1 1 1 1 1 1 1
2: 0 0 0 0 0 0 0 0 0
3: 1 1 1 1 1 1 1 1 1
4: 0 0 0 0 0 0 0 0 0
5: 0 0 0 0 0 0 0 0 0
6: 1 1 1 1 1 1 1 1 1
<小时>
with=FALSE
的使用在 ?data.table
中的 j
参数的文档中有很好的解释:
The use of with=FALSE
is nicely explained in the documentation for the j
argument in ?data.table
:
j:单个列名,单个列名表达式,list()
列名表达式,计算结果为列表的表达式或函数调用(包括 data.frame
和 data.table
也是列表),或者(当 with=FALSE
时)与 [.data.frame 中的 j 相同
.
j: A single column name, single expresson of column names, list()
of expressions of column names, an expression or function call that evaluates to list (including data.frame
and data.table
which are lists, too), or (when with=FALSE
) same as j in [.data.frame
.
从 v1.10.2 开始,也可以这样做:
From v1.10.2 onwards it is also possible to do this as follows:
keep <- setdiff(names(dt), "V9")
dt[, ..keep]
使用 ..
前缀符号将在调用范围(即全局环境)中查找,其值被视为列名或数字(来源).
Prefixing a symbol with ..
will look up in calling scope (i.e. the Global Environment) and its value taken to be column names or numbers (source).
这篇关于在 data.table 中选择列的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!