Hello again, I want to remove "$" sign and replace with nothing in my text. Therefore I used following code:> gsub("$|,", "", "$232,685.35436")[1] "$232685.35436" However I could not remove '$' sign. Can somebody help me why is it so? Thanks and regards
In regular expressions, "$" means the end of the line, so you have to escape it:> gsub("\\$|,", "", "$232,685.3567")[1] "232685.3567" On Thu, Mar 28, 2013 at 11:39 AM, Christofer Bogaso < bogaso.christofer@gmail.com> wrote:> Hello again, > > I want to remove "$" sign and replace with nothing in my text. > Therefore I used following code: > > > gsub("$|,", "", "$232,685.35436") > [1] "$232685.35436" > > > However I could not remove '$' sign. > > Can somebody help me why is it so? > > Thanks and regards > > ______________________________________________ > R-help@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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
On Mar 28, 2013, at 10:39 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Hello again, > > I want to remove "$" sign and replace with nothing in my text. > Therefore I used following code: > >> gsub("$|,", "", "$232,685.35436") > [1] "$232685.35436" > > > However I could not remove '$' sign. > > Can somebody help me why is it so? > > Thanks and regardsThe dollar sign is a metacharacter in regular expressions (see ?regex), thus has to be escaped to be interpreted as a literal character:> gsub("\\$|,", "", "$232,685.35436")[1] "232685.35436" Regards, Marc Schwartz
Hello, Try gsub("[$,]", "", "$232,685.35436") Hope this helps, Rui Barradas Em 28-03-2013 15:39, Christofer Bogaso escreveu:> Hello again, > > I want to remove "$" sign and replace with nothing in my text. > Therefore I used following code: > >> gsub("$|,", "", "$232,685.35436") > [1] "$232685.35436" > > > However I could not remove '$' sign. > > Can somebody help me why is it so? > > Thanks and regards > > ______________________________________________ > 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. >
Hi, If you just want to remove the $, it must be escaped because $ is a special character in regular expressions.> gsub("\\$", "", "$232,685.35436")[1] "232,685.35436" But it looks like you actually want to remove both $ and , Modifying your code:> gsub("\\$|,", "", "$232,685.35436")[1] "232685.35436" Or my preferred idiom:> gsub("[$,]", "", "$232,685.35436")[1] "232685.35436" Sarah On Thu, Mar 28, 2013 at 11:39 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Hello again, > > I want to remove "$" sign and replace with nothing in my text. > Therefore I used following code: > >> gsub("$|,", "", "$232,685.35436") > [1] "$232685.35436" > > > However I could not remove '$' sign. > > Can somebody help me why is it so? > > Thanks and regards >-- Sarah Goslee http://www.functionaldiversity.org