Home > Enterprise >  Grepl Over Every Column in Dataframe
Grepl Over Every Column in Dataframe

Time:07-15

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:

# 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!

CodePudding user response:

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

CodePudding user response:

If you want to use lapply, you can use the following code:

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

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

example[Reduce(`|`, lapply(example[-1], grepl, pattern="bc")),]
#>   id col1 col2
#> 1  1  abc  hbc
#> 2  2  cbe  lbc

Created on 2022-07-15 by the reprex package (v2.0.1)

  • Related