[R] How to remove $ (Dollar sign) from string

John Fox jfox at mcmaster.ca
Tue Apr 10 13:43:49 CEST 2012


Dear Nevil,

You have the escape the backslash:

> sub("\\$", "", "ABC$DEF")
[1] "ABCDEF"

I hope this helps,
 John

------------------------------------------------
John Fox
Sen. William McMaster Prof. of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/

On Tue, 10 Apr 2012 21:34:13 +1000
 Nevil Amos <nevil.amos at monash.edu> wrote:
> How do I remove a "$" character from a string sub() and gsub() with "$" or
> "\$" as pattern do not work.
> > sub("$","","ABC$DEF")
> [1] "ABC$DEF"
> > sub("\$","","ABC$DEF")
> Error: '\$' is an unrecognized escape in character string starting "\$"
> > sub(\$,"","ABC$DEF")
> Error: unexpected input in "sub(\"
> 
> Thanks
> -- 
> Nevil Amos
> Molecular Ecology Research Group
> Australian Centre for Biodiversity
> Monash University
> CLAYTON VIC 3800
> Australia
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list