【发布时间】:2018-07-20 13:59:04
【问题描述】:
我正在尝试对 data.table 进行子集化以选择列,但似乎无法弄清楚如何从向量中进行索引。
mtcars <- data.table(mtcars )
cols <- colnames(mtcars)[c(
grep( "a" , colnames( mtcars )) ,
which( colnames( mtcars ) =="mpg" )
)]
# this is an error
mtcars[ , cols ]
#this is a vector - not a subsetted data.table
mtcars[ , .( cols) ]
#this too
mtcars[ , eval(cols) ]
【问题讨论】:
-
你需要
mtcars[, ..cols],see here来解释 -
你总是可以做到
subset(mtcars, select = cols)
标签: r data.table