Мы можем разделить «Слова» на list
из vector
с, unnest
в «длинном» формате, удалить повторяющиеся строки, сгруппированные по «Год», «UID», paste
the ». Слова "в одну строку
library(dplyr)
df1 %>%
mutate(Words = strsplit(Words, ",")) %>%
unnest %>%
distinct(Year, UID, Words) %>%
group_by(UID, Year) %>%
summarise(Words = toString(Words))
# A tibble: 4 x 3
# Groups: UID [?]
# UID Year Words
# <dbl> <dbl> <chr>
#1 10 2009 ABC, CDEFGH, LMX, ABCD, IJKLM, PQRS, EFGH
#2 11 2010 BDFC, CDE, PQRS, ACCA, IJKLM
#3 12 2010 ABCD, CADDE
#4 12 2011 ABC, CDE, EFGH
данные
df1 <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 5), Year = c(2011, 2011,
2010, 2010, 2009, 2010, 2009), UID = c(12, 12, 11, 12, 10, 11,
10), Words = c("ABC,CDE", "EFGH,CDE", "BDFC,CDE,PQRS", "ABCD,CADDE",
"ABC,CDEFGH,LMX,ABCD,IJKLM,PQRS", "BDFC,ACCA,IJKLM", "EFGH")),
class = "data.frame", row.names = c(NA, -7L))