0

I have a column of alphanumeric data containing data that looks like this K*01:01+K*01:02:01:01, K*77:01:08+K*01:02:01:22, K*10:01:77. I want to separate the data based on the strings before and after the + sign, new data should be displayed in 2 separate columns. I want my output to look like this: column1 = K*01:01, K*77:01:08, K*10:01:77
column2 = K*01:02, K*01:02:01:02

I tried mydata$column1 <- sub("(.?)\+.", "\1", mydata$merged) works fine but when I used mydata$column2 <- sub(".\+(.?)", "\1", mydata$merged) for ID 3 K*10:01:77 is extracted both in columns 1 and 2, and I want column 2 to display a blank/empty cell for strings that do not have the + delimiter. Also I want the new columns to appear in the current data frame adjacent to the original merged column so packages like stringer do not work.

Mona
  • 93
  • 1
  • 10
  • 1
    Just use `str_split_fixed` from the `stringr` package: `str_split_fixed(x,fixed("+"),n=2)`. – nicola Aug 04 '17 at 06:49
  • Thank you nicola but the stringr package does not work as I want the new columns to appear in my current data frame adjacent to the original merged column – Mona Aug 04 '17 at 07:24

0 Answers0