use a character vector to filter a data

I have a data frame and I want to filter it from a list of values. Each of the values represents a partial string of characters. My data is structured like so:

str(my_actual_data)
spec_tbl_df [205,721 x 149] (S3: spec_tbl_df/tbl_df/tbl/data.frame)

How can I use a character vector to filter my data? For example:

# my target criteria (my character vector)
target <- c("a", "b", "c",)

# my data 
# 
my_data <- tribble(
  ~col1, ~col2, ~col3,
  "aaa", "aaa", "aaa",
  "bbb", "bbb", "bbb",
  "ccc", "ccc", "ccc",
  "ddd", "ddd", "ddd",
  "eee", "eee", "eee",
  "fff", "fff", "fff"
)

# If I had only one thing in my criteria I could use the following 
# and that would work fine:
my_filtered_data <- my_data %>%
    filter(str_detect(col1, "a"))

# If I had two I could add an additional operator to my filter 
# and that would work fine
my_filtered_data <- my_data %>%
    filter(str_detect(col1, "a") | str_detect(col1, "b") )

But I prefer to not have to keep adding to the filter because I have several more character strings to use; what's the solution?

# I have tried
target <- c("b", "d")
index <- str_detect(df$col1, target)
df[index, ]

Use the | character, meaning "or".

library(tidyverse)

# target criteria
target <- c("a", "b", "c") %>% paste(collapse = "|")

target
#> [1] "a|b|c"

# my data 
my_data <- tribble(
  ~col1, ~col2, ~col3,
  "aaa", "aaa", "aaa",
  "bbb", "bbb", "bbb",
  "ccc", "ccc", "ccc",
  "ddd", "ddd", "ddd",
  "eee", "eee", "eee",
  "fff", "fff", "fff"
)

# filter
my_filtered_data <- my_data %>%
  filter(str_detect(col1, target))

my_filtered_data
#> # A tibble: 3 x 3
#>   col1  col2  col3 
#>   <chr> <chr> <chr>
#> 1 aaa   aaa   aaa  
#> 2 bbb   bbb   bbb  
#> 3 ccc   ccc   ccc

Created on 2021-12-27 by the reprex package (v2.0.1)

2 Likes

An elegant and simple solution. Thank you. I was expecting a for loop.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.