如何从数据框架中删除列? [英] How do you remove columns from a data.frame?

查看:178
本文介绍了如何从数据框架中删除列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是那么多,你好吗?但是更多的你好吗?

Not so much 'How do you...?' but more 'How do YOU...?'

如果你有一个文件给你200列,你想减少到你需要分析的几个,你怎么看待?一个解决方案是否提供了优于另一个解决方案的优势?

If you have a file someone gives you with 200 columns, and you want to reduce it to the few ones you need for analysis, how do you go about it? Does one solution offer benefits over another?

假设我们有一列数据框col3,col2到col200。如果你只想要1-100然后125-135和150-200,你可以:

Assuming we have a data frame with columns col1, col2 through col200. If you only wanted 1-100 and then 125-135 and 150-200, you could:

dat$col101 <- NULL
dat$col102 <- NULL # etc

dat <- dat[,c("col1","col2",...)]

dat <- dat[,c(1:100,125:135,...)] # shortest probably but I don't like this

dat <- dat[,!names(dat) %in% c("dat101","dat102",...)]

我还缺少什么?我知道这是显而易见的主观,但它是其中一个棘手的事情,你可以潜水,开始做一个方法,并成为一种习惯,当有更有效的方式在那里。很像这个关于哪个的问题。

Anything else I'm missing? I know this is sightly subjective but it's one of those nitty gritty things where you might dive in and start doing it one way and fall into a habit when there are far more efficient ways out there. Much like this question about which.

编辑:

或者,是否有一个简单的方法来创建一个可行的列名称的向量? name(dat)不使用逗号在其间打印,您需要在上面的代码示例中,所以如果你打印出这样的名称,你有空格无处不在,必须手动放入逗号...有没有命令会给你col1,col2,col3,...作为你的输出,所以你可以轻松地抓住你想要的东西。

Or, is there an easy way to create a workable vector of column names? name(dat) doesn't print them with commas in between, which you need in the code examples above, so if you print out the names in that way you have spaces everywhere and have to manually put in commas... Is there a command that will give you "col1","col2","col3",... as your output so you can easily grab what you want?

推荐答案

我使用 data.table := 运算符立即删除列,无论表的大小如何。

I use data.table's := operator to delete columns instantly regardless of the size of the table.

DT[,coltodelete:=NULL]

DT[,c("col1","col20"):=NULL]

DT[,(125:135):=NULL]

DT[,(variableHoldingNamesOrNumbers):=NULL]

使用 < - code>或子集将复制整个表。 data.table := 操作符仅修改指向列的指针的内部向量。因此(几乎)即时操作。

Any solution using <- or subset will copy the whole table. data.table's := operator merely modifies the internal vector of pointers to the columns, in place. That operation is therefore (almost) instant.

这篇关于如何从数据框架中删除列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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