[R] gsub() and parenthesis symbols

Kieran Healy kjhealy at email.arizona.edu
Fri Aug 3 00:21:11 CEST 2001


Dear R-users --

I'm using R 1.3.0 on a PC running SuSE Linux 7.1. I'm confused by the
following behavior from the gsub() function. Am I doing something wrong?

## A string of characters
> string<-c("q","w","e","(",")","q","w","e")

## Use gsub to replace `q' with `A' 
> gsub("q","A",string)

[1] "A" "w" "e" "(" ")" "A" "w" "e" # Works fine

## Replace `(' [open-parenthesis symbol] with `A'
> gsub("(","A",string)

Error in gsub(pattern, replacement, x, ignore.case, extended) : 
	invalid regular expression

## But NB! -- Replace `)' [close parenthesis symbol] with `A'
> gsub(")","A",string)

[1] "q" "w" "e" "(" "A" "q" "w" "e" # Works fine!


## Further experimentation reveals the following:
## Replace open-paren with close-paren does not work 
> gsub("(",")",string)

Error in gsub(pattern, replacement, x, ignore.case, extended) : 
	invalid regular expression

## ... But replace close paren with open paren works fine!
> gsub(")","(",string)

[1] "q" "w" "e" "(" "(" "q" "w" "e"


My understanding is that parentheses are not special characters is
regexp syntax. And of course I don't understand at all why the open
paren symbol doesn't work but the close paren symbol does. It seems to
dislike it only when it appears as the first argument to the function.
Any help is much appreciated. 

Thanks,

Kieran

--
Kieran Healy, Assistant Professor,
Sociology Dept, University of Arizona.
kjhealy at u.arizona.edu :: (520) 621-3480
http://www.u.arizona.edu/~kjhealy
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list