Entering edit mode
2.1 years ago
yeluo
▴
40
I have the vector:
stri = c("Stage II", "Stage IVA", "Stage IIIB", "ACB" )
and I want to remove the A and B in "Stage IVA" ,"Stage IIIB", but not in "ACB",
I want to get
c("Stage II", "Stage IV", "Stage III", "ACB" )
Is there any method by using gsub?
You can have it simpler:
gsub("A$|B$", "", c("Stage II" , "Stage IVA" ,"Stage IIIB" ,"ACB" ))
That reads as "replace all A's and B's if they're the trailing character (that is what $ means, so last character).
Also, I recommend replacing whitespaces with something like underscore to have syntactically correct names in R.