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(.)))
>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().