Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading