I have a data frame that looks like this
df1
1. AB-CD-XY
2. AC-BE-DF-GH
I want to strip from the last hyphen to get something like this:
1. AB-CD
2. AC-BE-DF
This is my code
library(stringr)
ifelse(str_count(df1,'-')==3,
df_strip<- sub("^([^-]*-[^-]*-[^-]*).*", "\\1",df1),
df_strip<- sub("^([^-]*-[^-]*).*", "\\1", df1)
)
At the moment i get the result below which shows that only the else part of my code works. the sub
codes works separately on their own but not in the ifelse statement.
1. AB-CD
2. AC-BE