I would like to extract rows from a data frame on the basis of values from two (or more) columns, where the columns contain the same character strings. For example, in the data frame below, I would like to extract all the rows that contain “jam” and “fish”.
This is sort of close, but it focuses on a single column rather than selecting columns on 2 or more columns.
df <- structure(list(num = 1:4,
term_1 = c("jam", "bananna", "fish",
"carrot"),
term_2 = c("fish", "jam", "apple", "halva"),
term_3 = c("halva", "fish", "carrot", "fish")),
row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
df
#> num term_1 term_2 term_3
#> 1 1 jam fish halva
#> 2 2 bananna jam fish
#> 3 3 fish apple carrot
#> 4 4 carrot halva fish
I thought something like this might work, but it doesn't produce what I'm looking for.
df %>%
filter(term_1 == c("jam", "fish") | term_2 == c("jam", "fish") |
term_3 == c("jam", "fish"))
I’ve tried also tried some other filter() variants--along with if_all()--and str_select with filter(), but I'm missing some detail. Any direction will be appreciated.
Created on 2021-07-12 by the reprex package (v2.0.0)
Created on 2021-07-12 by the reprex package (v2.0.0)
1 jam fish halva
. I have no idea how to format this in the comments. Apologies. – avgoustisw