ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

stringr: find rows where any column content matches a regex

Consider the following example

> data_text <- data.frame(text = c('where', 'are', 'you'),
                        blob = c('little', 'nice', 'text'))
> data_text
# A tibble: 3 x 2
   text   blob
  <chr>  <chr>
1 where little
2   are   nice
3   you   text

I want to print the rows that contain the regex text (that is, row 3)

Problem is, I have hundreds of columns and I dont know which one contains this string. str_detect only work with one column at a time...

How can I do that using the stringr package? Thanks!

Upvotes: 4

Views: 3116

Answers (3)

Florian
Florian

Reputation: 25385

matches = apply(data_text,1,function(x) sum(grepl("text",x)))>0
result = data_text[matches,]

No other packages required. Hope this helps!

Upvotes: 4

alistaire
alistaire

Reputation: 43334

You can treat the data.frame as a list and use purrr::map to check each column, which can then be reduced into a logical vector that filter can handle. Alternatively, purrr::pmap can iterate over all the columns in parallel:

library(tidyverse)

data_text <- data_frame(text = c('where', 'are', 'you'),
                        blob = c('little', 'nice', 'text'))

data_text %>% filter(map(., ~.x == 'text') %>% reduce(`|`))
#> # A tibble: 1 x 2
#>    text  blob
#>   <chr> <chr>
#> 1   you  text

data_text %>% filter(pmap_lgl(., ~any(c(...) == 'text')))
#> # A tibble: 1 x 2
#>    text  blob
#>   <chr> <chr>
#> 1   you  text

Upvotes: 5

m0nhawk
m0nhawk

Reputation: 24168

With stringr and dplyr you can do this.

You should use filter_all from dplyr >= 0.5.0.

I have extended the data to have a better look on the result:

library(dplyr)
library(stringr)

data_text <- data.frame(text = c('text', 'where', 'are', 'you'),
                    one_more_text = c('test', 'test', 'test', 'test'),
                    blob = c('wow', 'little', 'nice', 'text'))

data_text %>%
  filter_all(any_vars(str_detect(., 'text')))

# output
  text one_more_text blob
1 text          test  wow
2  you          test text

Upvotes: 13

Related Questions