ssan
ssan

Reputation: 301

separate the unique value by comma in the list in a dataframe

I have a data frame with a column of list type where some of the values in the column looks exactly like below (including the c ):

c("A",B","A","A")

I want to convert the column into character type by unlisting it with the unique values separated with comma like below;

A,B

Tried to unlist the column like below but not able to get the desired output

df$col = unique(unlist(strsplit(as.character(df$col), ",")))

Upvotes: 0

Views: 363

Answers (1)

sempervent
sempervent

Reputation: 893

You can use the paste0() function to concatenate a vector of strings:

a <- c('A','B','A','A')
paste0(unique(a), collapse = ',')
[1] "A,B"

Upvotes: 1

Related Questions