An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130327/04fd7536/attachment.pl>
find and replace characters in a string
4 messages · Shane Carey, arun, Rui Barradas +1 more
txt<-? "LOI ."
gsub("[.]","%",txt)
#[1] "LOI %"
A.K.
From: Shane Carey <careyshan at gmail.com>
To: r-help at r-project.org
Sent: Wednesday, March 27, 2013 12:09 PM
Subject: [R] find and replace characters in a string
To: r-help at r-project.org
Sent: Wednesday, March 27, 2013 12:09 PM
Subject: [R] find and replace characters in a string
Hi,
I have a string of text as follows "LOI ."
How do I replace the dot with "(%)"
gsub(".","(%)",LOI .)
gives
"(%)(%)(%)(%)(%)"
Thanks
Shane ??? [[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.
Hello,
The period is a metacharacter so you have to escape it.
The period is escaped with a '\'. In it's turn, '\' is a metacharacter
so it needs to be escaped. Hence the double'\\'.
x <- "LOI ."
gsub("\\.", "(%)", x)
Hope this helps,
Rui Barradas
Em 27-03-2013 16:09, Shane Carey escreveu:
Hi,
I have a string of text as follows "LOI ."
How do I replace the dot with "(%)"
gsub(".","(%)",LOI .)
gives
"(%)(%)(%)(%)(%)"
Thanks
Although I am not an expert, this is simple.
txt<- "LOI ."
gsub(".","%",txt, fixed=TRUE)
Regards
Petr
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Rui Barradas
Sent: Wednesday, March 27, 2013 5:17 PM
To: Shane Carey
Cc: r-help at r-project.org
Subject: Re: [R] find and replace characters in a string
Hello,
The period is a metacharacter so you have to escape it.
The period is escaped with a '\'. In it's turn, '\' is a metacharacter
so it needs to be escaped. Hence the double'\\'.
x <- "LOI ."
gsub("\\.", "(%)", x)
Hope this helps,
Rui Barradas
Em 27-03-2013 16:09, Shane Carey escreveu:
Hi,
I have a string of text as follows "LOI ."
How do I replace the dot with "(%)"
gsub(".","(%)",LOI .)
gives
"(%)(%)(%)(%)(%)"
Thanks
______________________________________________ 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.