Question: This seems very simple but somehow can't figure out how to tackle this. What's the best way to detect if within a group there are two perticular string values in another column. 

Example df:

```
library(tidyverse)

tribble(
  ~city, ~var,
  "A", "PVDA",
  "A", "GL",
  "A", "GMBL",
  "B", "GL",
  "B", "VVD",
  "C", "CDA",
  "C", "VVD"
)

```

what I want to do is something like this:

```
join_anp_vgn_sf %>%
  group_by(city) %>%
  filter(grepl("^PVDA$&^GL$", var))

```

But that isn't working because that code is looking at each individual value. 

Desired output:

```
  city  var  
  <chr> <chr>
1 A     PVDA 
2 A     GL
3 A     GMBL 

```

Answer: 
Using `dplyr` 

```
df <- tribble(
  ~city, ~var,
  "A", "PVDA",
  "A", "GL",
  "B", "GL",
  "B", "VVD",
  "C", "CDA",
  "C", "VVD"
)

df %>% 
  group_by(city) %>% 
  filter(all(c("PVDA","GL") %in% var))

# A tibble: 2 x 2
# Groups:   city [1]
#   city  var  
#   <chr> <chr>
# 1 A     PVDA 
# 2 A     GL   

```

**Edit** 

With updated example

```
df <- tribble(
  ~city, ~var,
  "A", "PVDA",
  "A", "GL",
  "A", "GMBL",
  "B", "GL",
  "B", "VVD",
  "C", "CDA",
  "C", "VVD"
)

df %>% 
  group_by(city) %>% 
  filter(all(c("PVDA","GL") %in% var))

# A tibble: 3 x 2
# Groups:   city [1]
#   city  var  
#   <chr> <chr>
# 1 A     PVDA 
# 2 A     GL   
# 3 A     GMBL 

```
You can still use a `grepl` if you want so you can use partial string matching:

**Dplyr:**

```
df %>%
  group_by(city) %>%
  filter(sum(grepl("PVDA|GL", unique(var))) >= 2)

# A tibble: 2 x 2
# Groups:   city [1]
#   city   var
#  <chr> <chr>
#1     A  PVDA
#2     A    GL

```

**Base R:**

```
df[ave(df$var, df$city, FUN = function(x) sum(grepl("PVDA|GL", unique(x))) >= 2) %>% as.logical, ]

```