Identifying values in a dataframe that strings in one column share

I’m trying to find information that a set of strings in one column have in common. Here’s some mock data:

df1 <- data.frame(loc = c("Rufl","Rufl","Rufl","Rufl","Assp","Assp","Assp","Assp"),
                  name = c("ABC","DEF","GHI","JKL","ABC","PQR","RTU","JKL"),
                  date = c("2021-06-18", "2021-06-21", "2021-07-18", "2021-06-13", "2021-06-18", "2021-06-21", "2021-06-12", "2021-06-09"))

My ideal output would be something like this:

loc name date
Rufl ABC 2021-06-18
Aspp ABC 2021-06-18

Where I’m outputting the shared name and date between the two values in "loc". I’ve been trying a few methods with dplyr and can’t find the right syntax to get this output.

>Solution :

Are you looking for something like this:

library(dplyr)

df1 %>% 
  group_by(name, date) %>% 
  filter(n() > 1) %>%
  ungroup()

This returns

# A tibble: 2 x 3
  loc   name  date      
  <chr> <chr> <chr>     
1 Rufl  ABC   2021-06-18
2 Assp  ABC   2021-06-18

Leave a Reply