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

R concatenate only unique values in rows by ID

I have a dataframe like below and i am concatenating the IDs such that each Id should have one row. However, I am looking to summarize only the unique values. For example for region 1 and branch A, ID 1 has value "1" in both rows so output should be 1 instead of 1,1 but ID2 should be 1,2 as both values are unique. So only concatenate unique values.

Data <-
Region branch ID1 ID2 ID3 ID4
1 A 1 1 2 4
1 A 1 2 2 3
2 B 2 2 2 2
2 B 2 2 3 4

Desired Output
Region branch ID1 ID2 ID3 ID4
    1 A 1 1,2 2 4,3
    2 B 2 2 2,3 2,4

This is what I am trying

Data %>%
group_by(Region, branch) %>% 
  summarise(across(everything(), ~toString(.)))

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 :

How about

library(tidyverse)

Data %>% 
  group_by(Region, branch) %>% 
  summarise(
    across(
      everything(), 
      ~paste(as.list(unique(.)), collapse=","),
      .groups="drop"
    )
  ) 
# A tibble: 2 × 6
  Region branch ID1   ID2   ID3   ID4  
   <int> <chr>  <chr> <chr> <chr> <chr>
1      1 A      1     1,2   2     4,3  
2      2 B      2     2     2,3   2,4  

Edit

It also works without the as.list().

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