Haezer
Haezer

Reputation: 476

dplyr filter to get only the values that one of the groups take

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.

Upvotes: 2

Views: 56

Answers (2)

akrun
akrun

Reputation: 886938

We can also use slice

library(dplyr)
df %>% 
  slice(match(categ, categ[type == "A"]))
# A tibble: 7 x 2
#  type  categ 
#  <chr> <chr> 
#1 A     apple 
#2 A     pear  
#3 A     apple 
#4 A     banana
#5 A     pear  
#6 A     apple 
#7 A     banana

Upvotes: 1

tmfmnk
tmfmnk

Reputation: 39858

One possibility could be:

df %>% 
 filter(categ %in% categ[type == "A"])

  type  categ 
  <chr> <chr> 
1 A     apple 
2 A     pear  
3 A     apple 
4 A     banana
5 B     pear  
6 B     apple 
7 B     banana

Upvotes: 3

Related Questions