Delete a picture with gsub in r

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

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

Which is actually not right, but it should turn out like

 supp_matches2 # [1] "HONDA DIGITAL PLATEFORM" 

Why is this not as it should be?

+2
source share
2 answers

As I said in my comment, in your gsub("^.*[supp_matches1]", "", supp_matches) expression gsub("^.*[supp_matches1]", "", supp_matches) you are not actually using the supp_matches1 object, but only 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 pointed out by @rawr, you have there are brackets in your expression, so you will need to fix them. The correct expression to get what you need is sub("Project Change Request \\(PCR\\) - ", "", supp_matches)

To get what you want, you can use the fixed parameter of the gsub ( sub ) function, which says that the expression in the pattern parameter will match like that (so without having to avoid anything, but also no real regular expression).

So you are looking for:

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

Already @cathG provided the answer with fixed = TRUE. If you want to do everything with regex, you can 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" 

This is simply the rejection of all special characters present inside the variable that you want to use as the first parameter in the subfunction.

+3
source

All Articles