1

In R, I have a data frame of this kind:

df:

Col.1 Col.2
Apple ab1|cd2|ef3
Pear NULL
Peach mn7|op8|qr9

I am trying to extract the string between "|". In case of NULL, "NULL" (or an equivalent expression) should be displayed, obtaining:

df2:

Col.1 Col.2
Apple cd2
Pear NULL
Peach op8
It seems a very easy operation to me, but I have some problem with regular expressions. I guess something like this one should work:
sub_df <- gsub("(^.+|)("some expression for string bw |" )(|.+$)", "\2", df$Col.2)

If I understood correctly, this should separate each string in Col.2 in three parts, and keeps "part 2". Is that correct? And how to translate that "some expression for string bw |"?

Thank you very much for your help.

gmt
  • 93
  • 8

0 Answers0