无法使用数据表的一部分

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用数据表的一部分相关的知识,希望对你有一定的参考价值。

我的代码中有一个非常讨厌的问题:

library(data.table)
a<-(letters=c(1:20))
b<-rnorm(1:20)
c<-rnorm(1:20)
d<-rnorm(1:20)
final<-data.frame(a,b,c,d)

e<-data.table(final)
g<-e[, lapply(.SD, sum), by =c("a"), .SDcols = 2:4] #calculates a summary of columns for every "by" statement in my large dataframe
h<-g[,2:4]

向量h应该包括g的第2-4列,但它包含一个表示2:4的值。但是,在我的脚本的后面几行中,使用df [,columns]选择某些列是可行的。任何有关解决此问题的想法将不胜感激。

答案

编辑:

OP的问题在data.table的最新版本中没有意义,因为g[ , 2:4]可以按预期工作(返回data.table,其中包含g的所有行和第2至4列);不再需要with=FALSE。保留原始答案,因为它仍然有效。


Data Table FAQ中的第一个问题描述了此问题:(关于DT[,5]为什么返回5

Because, by default, unlike a data.frame, the 2nd argument is an 
expression which is evaluated within the scope of DT. 5 evaluates to 5.

并继续提供解决方法:

Having said this, there are some circumstances where referring to a column by
number is ok, such as a sequence of columns. In these situations just do:
DT[,5:10,with=FALSE] 

DT[,c(1,4,10),with=FALSE]

以上是关于无法使用数据表的一部分的主要内容,如果未能解决你的问题,请参考以下文章

片段中的按钮自定义视图

部分代码片段

过滤作为 ViewPager 片段一部分的 ListView

Android:NullPointerException 无法将数据库加载到片段内的列表视图中

GLSL-片段着色器不同部分的精度不同

Android YouTube Player API Fragment无法手动处理触摸事件