I know this is easy, but I am stumped:> gsub("0","K","8.00+00")[1] "8.KK+KK"> gsub("+","K","8.00+00")Error in gsub("+", "K", "8.00+00") : invalid regular expression '+' In addition: Warning message: In gsub("+", "K", "8.00+00") : regcomp error: 'Invalid preceding regular expression' I don't understand the error message. How do I go about replacing the "+" in the string "8.00+00" with another character? Tom -- View this message in context: http://www.nabble.com/Replacing-a-%22%2B%22-in-a-string-tp23655515p23655515.html Sent from the R help mailing list archive at Nabble.com.
You need to escape the '+' since it is used in regular expressions:> gsub("\\+","K","8.00+00 <file://+%22,%22k%22,%228.00+00/>")[1] "8.00K00">On Thu, May 21, 2009 at 11:45 AM, Tom La Bone <booboo@gforcecable.com>wrote:> > I know this is easy, but I am stumped: > > > gsub("0","K","8.00+00") > [1] "8.KK+KK" > > > gsub("+","K","8.00+00") > Error in gsub("+", "K", "8.00+00") : invalid regular expression '+' > In addition: Warning message: > In gsub("+", "K", "8.00+00") : > regcomp error: 'Invalid preceding regular expression' > > I don't understand the error message. How do I go about replacing the "+" > in > the string "8.00+00" with another character? > > Tom > > > > > -- > View this message in context: > http://www.nabble.com/Replacing-a-%22%2B%22-in-a-string-tp23655515p23655515.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Tom La Bone wrote:> I know this is easy, but I am stumped: > > >> gsub("0","K","8.00+00") >> > [1] "8.KK+KK" > > >> gsub("+","K","8.00+00") >> > Error in gsub("+", "K", "8.00+00") : invalid regular expression '+' > In addition: Warning message: > In gsub("+", "K", "8.00+00") : > regcomp error: 'Invalid preceding regular expression' > > I don't understand the error message. How do I go about replacing the "+" in > the string "8.00+00" with another character? >+ has a special meaning in regular expressions gsub( "[+]", "K", "8.00+00" ) gsub( "\\+", "K", "8.00+00" ) gsub( "+", "K", "8.00+00", fixed = TRUE ) see ?regex for details> Tom >-- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
Dear Tom, Use "\\" before "+": gsub("\\+","K","8.00+00") [1] "8.00K00" See ?regex. HTH, Jorge On Thu, May 21, 2009 at 11:45 AM, Tom La Bone <booboo@gforcecable.com>wrote:> > I know this is easy, but I am stumped: > > > gsub("0","K","8.00+00") > [1] "8.KK+KK" > > > gsub("+","K","8.00+00") > Error in gsub("+", "K", "8.00+00") : invalid regular expression '+' > In addition: Warning message: > In gsub("+", "K", "8.00+00") : > regcomp error: 'Invalid preceding regular expression' > > I don't understand the error message. How do I go about replacing the "+" > in > the string "8.00+00" with another character? > > Tom > > > > > -- > View this message in context: > http://www.nabble.com/Replacing-a-%22%2B%22-in-a-string-tp23655515p23655515.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Here are a few ways: gsub("[+]", "K", "8.00+00") gsub("\\+", "K", "8.00+00") gsub("+", "K", "8.00+00", fixed = TRUE) Note that with gsubfn you can replace several at once as it is like gsubfn but can take a replacement translation list: library(gsubfn) gsubfn(".", list("+" = " plus", "0" = " zero"), "8.00+00") # gives this: "8. zero zero plus zero zero" On Thu, May 21, 2009 at 11:45 AM, Tom La Bone <booboo at gforcecable.com> wrote:> > I know this is easy, but I am stumped: > >> gsub("0","K","8.00+00") > [1] "8.KK+KK" > >> gsub("+","K","8.00+00") > Error in gsub("+", "K", "8.00+00") : invalid regular expression '+' > In addition: Warning message: > In gsub("+", "K", "8.00+00") : > ?regcomp error: ?'Invalid preceding regular expression' > > I don't understand the error message. How do I go about replacing the "+" in > the string "8.00+00" with another character? > > Tom > > > > > -- > View this message in context: http://www.nabble.com/Replacing-a-%22%2B%22-in-a-string-tp23655515p23655515.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >