2013-07-04 68 views
-1
我有我的代碼真的是煩人的問題

無法使用的部件一個data.table

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應包括克2-4列,但它包含一個值2說: 4。但是,在我的腳本中,有些行還選擇了使用df [,columns]的某些列的作品。關於如何解決這個問題的任何想法都將不勝感激。

+2

您是否閱讀過data.table常見問題解答?改爲嘗試'g [,2:4,with = FALSE]'。 –

回答

3

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]