Split a column of character vectors and return a list

前端 未结 1 982
有刺的猬
有刺的猬 2021-01-22 07:28

I have the following dataframe:

df <- data.frame(Sl.No = c(1:6),
                 Variable = c(\'a\', \'a,b\', \'a,b,c\', \'b\', \'c\', \'b,c\'))


         


        
1条回答
  •  佛祖请我去吃肉
    2021-01-22 08:14

    We can split the Variable column at "," and get all the values and select only the unique ones.

    unique(unlist(strsplit(df$Variable, ",")))
    #[1] "a" "b" "c"
    

    If the Variable column is factor convert it into character before using strsplit.

    0 讨论(0)
提交回复
热议问题