我正在尝试将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) ]
答案 0 :(得分:0)
在dplyr中,这将返回所需的结果:
mtcars <- mtcars %>% dplyr:: select(contains("a"), "mpg")