选择data.table R中的列子集 [英] Select subset of columns in data.table R

查看:188
本文介绍了选择data.table R中的列子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有很多列的数据表,例如:

  dt <-data.table (10 * 10),10,10))

我想对数据表执行一些操作,例如产生相关矩阵( cor(dt))。为了做到这一点,我要删除一些包含非数字值或超出一定范围的值的列。



假设我想找到相关矩阵不包括V1,V2,V3和V5。



这是我目前的方法:

  cols<  - ! colnames(dt)==V2| colnames(dt)==V3| colnames(dt)==V5)
new_dt< -subset ,cols)
cor(new_dt)



我觉得这很麻烦,表语法通常是如此优雅。是否有更好的方法这样做?

解决方案

使用 with = FALSE

  cols = paste(V,c(1,2,3,5),sep =) 

dt [,!cols,with = FALSE]

通过数据表简介插页。






更新: v1.10.2 ,您也可以:

  dt [,..cols] 
pre>

查看v1.10.2下的第一个新闻项目此处了解详情。


I have a data table with a bunch of columns, e.g.:

dt<-data.table(matrix(runif(10*10),10,10))

I want to perform some operation on the data table, such as producing a correlation matrix (cor(dt)). In order to do this, I want to remove a few columns that contain non-numeric values or values outside a certain range.

Let's say I want to find the correlation matrix excluding V1, V2, V3 and V5.

Here is my current approach:

cols<-!(colnames(dt)=="V1" | colnames(dt)=="V2" | colnames(dt)=="V3" | colnames(dt)=="V5")
new_dt<-subset(dt,,cols)
cor(new_dt)

I find this pretty cumbersome, considering data.table syntax is usually so elegant. Is there a better method of doing this?

解决方案

Use with=FALSE:

cols = paste("V", c(1,2,3,5), sep="")

dt[, !cols, with=FALSE]

I suggest going through the "Introduction to data.table" vignette.


Update: From v1.10.2 onwards, you can also do:

dt[, ..cols]

See the first NEWS item under v1.10.2 here for additional explanation.

这篇关于选择data.table R中的列子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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