Remove duplicates in string

后端 未结 3 827
清歌不尽
清歌不尽 2021-01-14 06:36

I have the following data set

df <- data.frame(
    path = c(\"a,b,a\", 
        \"(direct) / (none),   (direct) / (none), google / cpc,    google / cpc\"         


        
3条回答
  •  -上瘾入骨i
    2021-01-14 06:45

    Basic logic behind below code :

    i)split each row on "," , (ii) remove whitespace (iii) take unique values
    (iv) collapse back on "," and paste

    t = apply(df, 1, function(x) paste0(unique(trimws(unlist(strsplit(x,",")))), collapse = ","))
    df=data.frame(t)
    # df
    #                               t
    #1                            a,b
    #2 (direct) / (none),google / cpc
    #3                            f,d
    #4                            a,c
    

提交回复
热议问题