[R] Escaping " ' " character

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Mon Aug 21 22:43:54 CEST 2006


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.



The reason I am asking is, I have to run another
function to make PREPARE statements for postgresql. 

Example:
"EXECUTE fetch_count_fterm_sql('hydrolase activity');"
                                                      
                 "EXECUTE fetch_count_fterm_sql('actin
binding');"
"EXECUTE
fetch_count_fterm_sql('3',5'-cyclic-nucleotide
phosphodiesterase activity');"

Here the last query statement will raise an exception
and my program fails from there. 

for instance:

mydb=# EXECUTE
fetch_count_fterm_sql('3',5'-cyclic-nucleotide
phosphodiesterase activity');
ERROR:  syntax error at or near "'-cyclic-nucleotide
phosphodiesterase activity'" at character 36



mydb=# EXECUTE
fetch_count_fterm_sql('3\',5\'-cyclic-nucleotide
phosphodiesterase activity');
 count
-------
    17
(1 row)



More information about the R-help mailing list