【问题标题】:Subsetting r data.table with grep [duplicate]用 grep 设置 r data.table [重复]
【发布时间】: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


【解决方案1】:

在 dplyr 中,这将返回所需的结果: mtcars &lt;- mtcars %&gt;% dplyr:: select(contains("a"), "mpg")

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 2015-04-29
    相关资源
    最近更新 更多