简体   繁体   English

在R中使用like运算符删除列名称

[英]Removing Column names using like operator in R

I have a dataframe where most of the column names are as below: 我有一个数据框,其中大多数列名称如下:

tre_ui_1920
tre_ui_2221
tre_ui_8989

and something like 和类似的东西

foo_bar_123
foo_bar_456

I want to delete all the columns belonging to foo_bar_* and tre_ui_* 我想删除属于foo_bar_*tre_ui_*所有列

I have seen few codes in R which suggest to use subset and indexing. 我在R中很少看到建议使用子集和索引的代码。 Is there any better way to do this? 有什么更好的方法吗?

We can use grepl to return a logical index based on the patterns in the column names 我们可以使用grepl根据列名中的模式返回逻辑索引

i1 <- !grepl("foo_bar_|tre_ui_", names(df1))
subdf1 <- df1[i11]

这很容易在dplyr完成:

dat %>% select(matches("foo_bar_|tre_ui_"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM