I would like to filter to get the rows from any group of my tibble, where one of the variable matches the values taken by one of the groups.
structure(list(type = c("A", "A", "A", "A", "B", "B", "B", "B",
"B"), categ = c("apple", "pear", "apple", "banana", "pear", "kiwi",
"apple", "banana", "mango")), .Names = c("type", "categ"), row.names = c(NA,
-9L), class = c("tbl_df", "tbl", "data.frame"))
# type categ
# <chr> <chr>
# A apple
# A pear
# A apple
# A banana
# B pear
# B kiwi
# B apple
# B banana
# B mango
I want to get the rows from any group where categ is one of the value taken by categ in the group A for type
Basically I want to do this :
df %>%
filter(categ %in% pull(filter(df, type == 'A'), categ))
# type categ
# <chr> <chr>
# A apple
# A pear
# A apple
# A banana
# B pear
# B apple
# B banana
this is an ugly way to do that and I'm pretty sure there is a far better way but I can't find it out.
Thanks for help and sorry for bad english.