-2

I have a string that looks like:

exampleList <- c("CO-0891-VI05-T1-1", "CO-0891-VI05-T1-1-166", "CO-0891-VI05-T1-2", "CO-0891-VI05-T1-2-177", "AI-0022-FR07A-T1-1", "AI-0022-FR07A-T1-1-10")

I wish to remove everything after the last underscore (-) so my result looks something like this:

exampleList <- c("CO-0891-VI05-T1-1", "CO-0891-VI05-T1-1", "CO-0891-VI05-T1-2", "CO-0891-VI05-T1-2", "AI-0022-FR07A-T1-1", "AI-0022-FR07A-T1-1") 

I tried

mutate(test = str_remove(exampleList, "$ - "))
user438383
  • 5,716
  • 8
  • 28
  • 43
fdpr
  • 1
  • 1
  • 1
    `str_remove(exampleList, "-[^-]*$")` should work. FYI, `-` is called a "dash", an underscore is `_`. – Gregor Thomas Aug 29 '23 at 15:13
  • This is almost a verbatim post as this: https://stackoverflow.com/questions/44687333/remove-characters-after-the-last-occurrence-of-a-specific-character The reason it says "underscore" and not "dash" is bc that's what the original post has and it was copied and pasted – jpsmith Aug 29 '23 at 15:14

0 Answers0