Home > Back-end >  Replace string containing parantheses
Replace string containing parantheses

Time:10-13

I have a vector that looks like this:

vector <- c("SPN.subset(RELN).geneset1", "Myeloid.svz.geneset1")

I want to replace "SPN.subset(RELN).geneset1" with "SPN.subset(RELN).caudate.geneset1", but it seems the parantheses aren't allowing this to happen:

e.g

> gsub("SPN.subset(RELN).geneset1", "SPN.subset(RELN).caudate.geneset1", vector)
[1] "SPN.subset(RELN).geneset1" "Myeloid.svz.geneset1"  

Any ideas how to fix this?

CodePudding user response:

We may match the closing parentheses, followed by the ., andi. nthe replacement, add the ). followed by the caudate substring

sub("\\)\\.", ").caudate.", vector)
[1] "SPN.subset(RELN).caudate.geneset1" "Myeloid.svz.geneset1"         

with gsub/sub, the default option is fixed = FALSE i.e. in regex mode. When it is in regex mode, the () is treated differently i.e. it is a metacharacter to capture group instead of literally evaluating

CodePudding user response:

We could escape the special characters with \\ in the pattern of gsub: Notice akrun is providing a more general approach!

gsub("SPN.subset\\(RELN\\).geneset1", "SPN.subset(RELN).caudate.geneset1", vector)
[1] "SPN.subset(RELN).caudate.geneset1" "Myeloid.svz.geneset1"    
  •  Tags:  
  • r
  • Related