Removing a pattern With gsub in r

元气小坏坏 提交于 2019-12-24 03:48:36

问题


I have a string Project Change Request (PCR) - HONDA DIGITAL PLATEFORM saved in supp_matches, and supp_matches1 contains the string Project Change Request (PCR) -.

supp_matches2 <- gsub("^.*[supp_matches1]","",supp_matches)
supp_matches2
# [1] " (PCR) - HONDA DIGITAL PLATEFORM"

Which is actually not correct but it should come like

supp_matches2
# [1] "HONDA DIGITAL PLATEFORM"

Why is it not coming the way it should be?


回答1:


As I say in my comment, in your expression gsub("^.*[supp_matches1]", "", supp_matches), you're not really using the object supp_matches1 but just the letters inside it.

You could do something like gsub(paste0("^.*", supp_matches1), "", supp_matches) to really use the expression contained in supp_matches1, except that, as mentionned by @rawr, you have parentheses in your expression so you would need to excape them.
The correct expression to get what you want would then be sub("Project Change Request \\(PCR\\) - ", "", supp_matches)

To get what you want, you can use the fixed parameter of gsub (sub) function, which is saying that the expression in the parameter pattern will be matched as it is (so, without the need to escape anything, but also, no real regular expression).

So what's you are looking for is :

gsub(supp_matches1, "", supp_matches, fixed=TRUE) # or just with `sub` in this case
#[1] "HONDA DIGITAL PLATEFORM"



回答2:


Already @cathG provided an answer with fixed=TRUE. If you want to do all with regex, then you may try this.

> w1 <- "Project Change Request (PCR) - HONDA DIGITAL PLATEFORM"
> w2 <- "Project Change Request (PCR) - "
> sub(paste0("^", gsub("(\\W)", "\\\\\\1", w2)), "", w1)
[1] "HONDA DIGITAL PLATEFORM"

It's just a kind of escaping all the special chars present inside the variable you want to use as first parameter in sub function.



来源:https://stackoverflow.com/questions/31267208/removing-a-pattern-with-gsub-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!