4

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 from it to achieve this:

A B C
1 M 1,2
2 M 1,5
3 M 4,5,7
ChrisYee90
  • 389
  • 1
  • 6
  • 12

1 Answers1

6

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
zx8754
  • 52,746
  • 12
  • 114
  • 209
MrFlick
  • 195,160
  • 17
  • 277
  • 295