0

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_*

I have seen few codes in R which suggest to use subset and indexing. Is there any better way to do this?

camille
  • 16,432
  • 18
  • 38
  • 60
Rudra
  • 149
  • 1
  • 2
  • 11
  • Try `i1 <- !grepl("foo_bar_|tre_ui_", names(df1)); df1[i11]` – akrun Apr 09 '19 at 12:20
  • It's easier to help if you include [a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), particularly a sample of data, but also what you've tried that hasn't worked. You say "is there any better way," so what are those ways that haven't worked so far? How does this differ from similar questions elsewhere? – camille Apr 09 '19 at 12:29
  • For example, here's one: https://stackoverflow.com/q/51846437/5325862 – camille Apr 09 '19 at 12:34

2 Answers2

1

We can use grepl to return a logical index based on the patterns in the column names

i1 <- !grepl("foo_bar_|tre_ui_", names(df1))
subdf1 <- df1[i11]
akrun
  • 874,273
  • 37
  • 540
  • 662
0

This is easily done in dplyr:

dat %>% select(matches("foo_bar_|tre_ui_"))
SCDCE
  • 1,603
  • 1
  • 15
  • 28