Selecting only unique values from a comma separated string

前端 未结 1 1007
难免孤独
难免孤独 2020-12-11 23:22

I have a data frame that looks like this:

A B C
1 M 1,2
2 M 1,5,5,5
3 M 4,5,7,7

I want to take column C and select only the unique value fr

相关标签:
1条回答
  • 2020-12-11 23:47

    Assuming your C column is a character vector as in this sample

    dd <- read.table(text="A B C
    1 M 1,2
    2 M 1,5,5,5
    3 M 4,5,7,7", header = TRUE, stringsAsFactors = FALSE)
    

    You can use strsplit to split the column values on a comma, and then reassemble the unique values with this code:

    dd$D <- sapply(strsplit(dd$C, ",", fixed = TRUE), function(x) 
        paste(unique(x), collapse = ","))
    dd
    #   A B       C     D
    # 1 1 M     1,2   1,2
    # 2 2 M 1,5,5,5   1,5
    # 3 3 M 4,5,7,7 4,5,7
    
    0 讨论(0)
提交回复
热议问题