在数据表(R)中打印列的子集 [英] Printing a subset of columns in a data table (R)

查看:133
本文介绍了在数据表(R)中打印列的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印数据表 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:列名的表达式的单个列名,列名的单个表达式, list(),表达式或函数调用,列表(也包括 data.frame data.table ),

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.

这篇关于在数据表(R)中打印列的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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