0

data frame

I want to subset my data frame to contain all elements that contain the word 'inhibitor'. I want to keep the entire element. For example, I'd have a new data frame with: 342 RENIN INHIBITORS, 342 RENIN INHIBITORS, 216 ALPHA-GLUCOSIDASE INHIBITORS, etc.

This doesn't work

library(dplyr)
TC1S1 = read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/TC1S1.csv')
x <- TC1S1 %>% filter(grepl('inhibitor', TC1S1[,1]))
bandcar
  • 649
  • 4
  • 11
  • We cannot read data into R from images. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(h)`, if that is not too large. – neilfws Aug 19 '22 at 03:23

2 Answers2

0

We can use apply() in row mode along with grepl(), for a base R option:

a[apply(a, 1, function(r) any(grepl("inhibitor", r, fixed=TRUE))), ]

  col1            col2            col3
1 drug  drug-inhibitor drug inhibitor3
2 drug drug inhibitor2 drug inhibitor4

Data:

a <- data.frame(col1=c('drug', 'drug', 'drug'),
                col2=c('drug-inhibitor', 'drug inhibitor2', 'drug'),
                col3=c('drug inhibitor3', 'drug inhibitor4', 'drug'))
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You could also use str_detect(), e.g.

library(dplyr)
library(stringr)
a <- data.frame(
  col1 = c("drug", "drug", "drug"),
  col2 = c("drug-inhibitor", "drug inhibitor2", "drug"),
  col3 = c("drug inhibitor3", "drug inhibitor4", "drug")
)

a %>%
  filter(if_any(everything(), ~ stringr::str_detect(string = ., pattern = "inhibitor")))

Output:

  col1            col2            col3
1 drug  drug-inhibitor drug inhibitor3
2 drug drug inhibitor2 drug inhibitor4
Julian
  • 6,586
  • 2
  • 9
  • 33