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, ]