在data.table中选择列的子集 [英] Selecting a subset of columns in a data.table

查看:139
本文介绍了在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"? 

使用数据框的方式通过代码执行此操作:

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:单个列名,单个列名的expresson, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆