[R] Escaping " ' " character

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Tue Aug 22 16:32:05 CEST 2006


Dear marc: thank you for your tip. 

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

here cat is printing on screen. 

how can I direct the output to an object. 

I cannot do:

y <- cat(x.new, "\n")

is there any other way.

thanks
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