【发布时间】: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