[R] Escaping " ' " character

Marc Schwartz (via MN) mschwartz at mn.rr.com
Mon Aug 21 23:51:53 CEST 2006


On Mon, 2006-08-21 at 13:43 -0700, Srinivas Iyyer wrote:
> Dear all:
> 
> I have a character object x with ' (single-quote)
> character.
> 
> x <- c('"hydrolase activity","actin
> binding","3',5'-cyclic-nucleotide phosphodiesterase
> activity")
> 
> I want to write a function that will identify ' and
> replaces with \' 
> 
> myf <- function(term){
> if (grep("'",term))
> { sub("'","\'",term)}
>  }
> > myf(x)
> [1] "hydrolase activity"
> [2] "actin binding"
> [3] "3',5'-cyclic-nucleotide phosphodiesterase
> activity"
> 
> 
> In the result "3',5'" is remains unchaned. I expected
> that to "3\',5\'-cyclic-nucleotide phosphodiesterase
> activity". 
> 
> Could any one help me here. 
> Thank you.

<snip>

Srini,

Try this:

 x <- "3',5'-cyclic-nucleotide phosphodiesterase activity"

> x
[1] "3',5'-cyclic-nucleotide phosphodiesterase activity"

> gsub("'", "\\\\'", x)
[1] "3\\',5\\'-cyclic-nucleotide phosphodiesterase activity"


Note that I use gsub() to replace both instances of the ', whereas sub()
will only replace the first.

The escape character itself needs to be escaped when used within the
replacement regex, since the "\" is a metacharacter. You end up with
four "\"s since R also treats the "\" specially.

When you cat() the output, you get:

> x.new
[1] "3\\',5\\'-cyclic-nucleotide phosphodiesterase activity"

> cat(x.new, "\n")
3\',5\'-cyclic-nucleotide phosphodiesterase activity


HTH,

Marc Schwartz



More information about the R-help mailing list