Я предлагаю вам решение с использованием data.table:
unique_counts <- function(str){
return(uniqueN(unlist(strsplit(gsub(" ", "" ,str), ","))))
}
unique_strings <- function(str){
return(paste0(unique(unlist(strsplit(gsub(" ", "" ,str), ","))), collapse=","))
}
a <- c("cat, cat, dog", "dog")
b<- c("cat")
c<- c("dog, dog", "cat")
df <- data.frame(position= c("1","2"),a, b, c, stringsAsFactors = F)
df <- as.data.table(df)
for (i in colnames(df)[2:length(colnames(df))]){
df[ , eval(paste0(i,"_uniq")):=mapply(unique_strings, get(i))]
df[ , eval(paste0(i,"_uniq_counts")):=mapply(unique_counts, get(i))]
df[ , eval(i):=NULL]
}
Best!