Question

I have a string which is like

adgroupname <-"life sciences | medical device" 

and I want to replace

" | " with "|" 

and the output should be like

life sciences|medical device 

I am using

adgroupname <- gsub(pattern=" | ",replacement="|",x=adgroupname)

But the output shows up as

life|sciences|||medical|device

How to do it using R ? new to R.any help is appreciated.

Was it helpful?

Solution

You need to escape the pipe symbol:

adgroupname<-gsub(" \\| ", "|", adgroupname)

Result:

> adgroupname <-"life sciences | medical device" 
> adgroupname
[1] "life sciences | medical device"
> adgroupname<-gsub(" \\| ", "|", adgroupname)
> adgroupname
[1] "life sciences|medical device"

OTHER TIPS

The | symbol is a special character for regular expressions, and must be escaped. This symbol means to or the two possibilities. Here, you're saying to replace a space or a space with a |. That is, replace each space with a |, which is what it did.

I prefer to use a character class to an escape, which also works:

gsub(pattern=" [|] ",replacement="|",x=adgroupname)
## [1] "life sciences|medical device"

Same result, but you have fewer backslashes to count.

It's not the case here, but when you must pass the regular expression through a shell to another process, counting the backslashes gets very tedious.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top