Cumulative string concatenation of unique values

I'm trying to create a running total of only the unique values of concatenated strings. I can't figure out how to do it using tidyverse functions and principles.

Given the below data frame that initially contains columns col1 and col2 how do I apply functions to col2 that will return the desired output as shown in col3 In pseudo-code the general idea of what I want is this:
paste0(col3[i-1], ',', col3[i][which(!(col3[i] %in% col3[i-1]))])
but in tidyverse principles and that actually works.

df1 <- data.frame(col1= c(1, 2, 3, 4, 5),
           col2 = c('apple,carrot', 'banana,carrot', 'grape,blueberry,mango', 'coconut', 'grape,apple'),
           col3 = c('apple,carrot', 'apple,carrot,banana', 'apple,carrot,banana,grape,blueberry,mango', 'apple,carrot,banana,grape,blueberry,mango,coconut', 'apple,carrot,banana,grape,blueberry,mango,coconut'))

Hi,

expand.grid and paste:

fruits <- c("APPLE", "CARROTS", "BANANA", "MANGO")

coconuts <- c("Coconut1", "Coconut2", "Coconut3", "Coconut4", "Coconut5",
"Coconut6", "Coconut7", "Coconut8", "Coconut9", "Coconut10",
"Coconut11", "Coconut12", "Coconut13")

deck <- expand.grid(fruit=fruits, coconut=coconuts)

deck <- paste(deck$fruit, deck$coconut)

deck

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.