Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grepl Over Every Column in Dataframe

I have this dataframe in R:

col1 = c("abc", "cbe", "ddd")
col2 = c("hbc", "lbc","uhr")
id = c(1,2,3)

example = data.frame(id, col1, col2)

I want to select all rows that contain "bc" in any column. This would look like this:

 id col1 col2
1  1  abc  hbc
2  2  cbe  lbc

I know how to do this for a single column:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# almost works
select_col_1 = example[grepl("bc", example$col1, ignore.case = TRUE)]
  • But is there a way to repeat this for every column in the data frame?

I tried to do this with the "lapply" statement:

lapply(example, function(x){x[grepl('bc', example, ignore.case = TRUE)])

But I don’t think I am doing this correctly.

Can someone please show me how to do this?

Thank you!

>Solution :

You can do this using the dplyr package which supplies filter() to select rows, everything() to select all columns and if_any() for a result in any column.

library(dplyr)
example %>% 
  filter(if_any(everything(), ~ grepl("bc", .)))

Result:

  id col1 col2
1  1  abc  hbc
2  2  cbe  lbc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading