Remove some elements from a string
So, I have a vector consisting of
data<-c("Mark And (BD Marketing Da 1 Z _ 9793)",
"Andre All (BD Marketing DA 1 Z _ 9794 (plus))",
"Alli Inn (BD Sport Educ 1 C _ 9722 (plus))",
"Alli Inn (BP Sport Educ 1 Z _ 9347)")
And now I need to remove all characters before the _ as well as the parentheses where the word (plus) is missing, so the result should be
Mark And BD Marketing Da 1 Z
Andre All BD Marketing DA 1 Z (plus)
Alli Inn BD Sport Educ 1 C (plus)
Alli Inn BP Sport Educ 1 Z
I used
gsub("\\s*\\w*$", "", data)
and got
Alli Inn (BP Sport Educ 1 Z
but this is not correct as I need to remove the other parenthesis and also keep the (plus) where it is written.
I tried this:
gsub('\((?!plus)|(?<!plus)\)|.\\d+', '', rownames(data), perl=TRUE)
and got this
Alli Inn BP Sport Educ Z
but now I am missing number 1 to letter
+3
source to share