【问题标题】:Removing columns from a data.table using dplyr使用 dplyr 从 data.table 中删除列
【发布时间】:2015-07-07 18:23:18
【问题描述】:

我正在尝试使用 dplyr 包中的 select 函数删除 data.table 中的列,并且当没有列与我的搜索模式匹配时遇到错误。

使用标准的iris 数据集并导入相应的包,

library(dplyr)
library(data.table)
select(data.table(iris), -matches("^Petal"))
# returns a table with three columns: Sepal.Length, Sepal.Width, Species
select(data.table(iris), -matches("^ER[[:digit:]]+$"))
# displays error message:
# Error in data.table::setnames(out, names(vars)) : x has no column names

有什么方法可以删除与给定正则表达式匹配的任何列(如果它们存在),否则不修改表?

【问题讨论】:

    标签: regex r data.table dplyr


    【解决方案1】:

    试试这个

     iris[, !grepl("^ER[[:digit:]]+$", names(iris))]
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2019-05-18
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多