I have a df that looks like the follow:
ID LOC
1 A
1 A
2 A
2 B
3 A
3 A
3 A
4 A
4 B
4 C
What I would like to do is collapse the LOC values to one row per ID and if they are the same keep it as one value, so my output would be:
ID LOC
1 A
2 A + B
3 A
4 A + B + C
Right now I am using:
group_by(ID) %>%
mutate(concat_LOC = paste0(LOC, collapse = " + "))
which concats all values even if they are the same
summarize instead of mutate.LOC values, use unique.dat %>%
group_by(ID) %>%
summarize(LOC = paste(unique(LOC), collapse = " + ")) %>%
ungroup()
# # A tibble: 4 x 2
# ID LOC
# <int> <chr>
# 1 1 A
# 2 2 A + B
# 3 3 A
# 4 4 A + B + C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With