Greetings R-ians:
I know what doesn?t work but I don?t know why, nor how to remedy things.
I have a character string containing "." which I want to replace with
" "
gsub(".", " ", file.label) replaces the every character with
a blank.
However gsub(".xls", " ", file.label) replaces
".xls" with a blank as
expected.
It appears that "." is some kind of wild-card. How do I tell gsub
that a
period is just a period?
Thanks.
Charles Annis, P.E.
Charles.Annis at StatisticalEngineering.com
phone: 561-352-9699
eFax:? 614-455-3265
http://www.StatisticalEngineering.com
?
on 09/23/2008 12:16 PM Charles Annis, P.E. wrote:> Greetings R-ians: > > I know what doesn?t work but I don?t know why, nor how to remedy things. > > I have a character string containing "." which I want to replace with " " > > gsub(".", " ", file.label) replaces the every character with a blank. > > However gsub(".xls", " ", file.label) replaces ".xls" with a blank as > expected. > > It appears that "." is some kind of wild-card. How do I tell gsub that a > period is just a period? > > Thanks.A period is indeed a wild card, the interpretation meaning any character. In your second example, the interpretation would be replace 'xls', preceded by any character, with a space: > gsub(".xls", " ", "xlsAxls.xls") [1] "xls " To specify a period as an explicit character, you need to escape it, which in R means double the escape character: > gsub("\\.xls", " ", "xlsAxls.xls") [1] "xlsAxls " HTH, Marc Schwartz
Thanks!
Charles Annis, P.E.
Charles.Annis at StatisticalEngineering.com
phone: 561-352-9699
eFax: 614-455-3265
http://www.StatisticalEngineering.com
-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu]
Sent: Tuesday, September 23, 2008 1:30 PM
To: Charles Annis, P.E.
Subject: Re: [R] gsub difficulty
Charles -
Here are two ways:
> string = 'one.two.three'
> gsub('\\.',' ',string)
[1] "one two three"> gsub('.',' ',string,fixed=TRUE)
[1] "one two three"
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Tue, 23 Sep 2008, Charles Annis, P.E. wrote:
> Greetings R-ians:
>
> I know what doesn?t work but I don?t know why, nor how to remedy things.
>
> I have a character string containing "." which I want to replace
with " "
>
> gsub(".", " ", file.label) replaces the every character
with a blank.
>
> However gsub(".xls", " ", file.label) replaces
".xls" with a blank as
> expected.
>
> It appears that "." is some kind of wild-card. How do I tell
gsub that a
> period is just a period?
>
> Thanks.
>
>
> Charles Annis, P.E.
>
> Charles.Annis at StatisticalEngineering.com
> phone: 561-352-9699
> eFax:? 614-455-3265
> http://www.StatisticalEngineering.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.
>
Thanks, Jorge, for another alternative.
Charles Annis, P.E.
<mailto:Charles.Annis@StatisticalEngineering.com>
Charles.Annis@StatisticalEngineering.com
phone: 561-352-9699
eFax: 614-455-3265
<http://www.StatisticalEngineering.com>
http://www.StatisticalEngineering.com
_____
From: Jorge Ivan Velez [mailto:jorgeivanvelez@gmail.com]
Sent: Tuesday, September 23, 2008 2:21 PM
To: Charles.Annis@statisticalengineering.com
Subject: Re: [R] gsub difficulty
Dear Charles,
Phil Spector answered your original question by using two different
approaches, but here I'm going again :)
x="your.string.is.here"
gsub("[.]"," ",x)
[1] "your string is here"
HTH,
Jorge
On Tue, Sep 23, 2008 at 1:23 PM, Jorge Ivan Velez
<jorgeivanvelez@gmail.com>
wrote:
Deat Charles,
Is this what you want?
x=".xls"
sub(".","",x)
[1] "xls"
HTH,
Jorge
On Tue, Sep 23, 2008 at 1:16 PM, Charles Annis, P.E.
<Charles.Annis@statisticalengineering.com> wrote:
Greetings R-ians:
I know what doesn't work but I don't know why, nor how to remedy things.
I have a character string containing "." which I want to replace with
" "
gsub(".", " ", file.label) replaces the every character with
a blank.
However gsub(".xls", " ", file.label) replaces
".xls" with a blank as
expected.
It appears that "." is some kind of wild-card. How do I tell gsub
that a
period is just a period?
Thanks.
Charles Annis, P.E.
Charles.Annis@StatisticalEngineering.com
phone: 561-352-9699
eFax: 614-455-3265
http://www.StatisticalEngineering.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]]
Any of these will replace every dot with a space:
x <- "a.b.c"
gsub("\\.", " ", x)
gsub("[.]", " ", x)
gsub(".", " ", x, fixed = TRUE)
chartr(".", " ", x)
See ?regex for more info on regular expressions.
On Tue, Sep 23, 2008 at 1:16 PM, Charles Annis, P.E.
<Charles.Annis at statisticalengineering.com>
wrote:> Greetings R-ians:
>
> I know what doesn't work but I don't know why, nor how to remedy
things.
>
> I have a character string containing "." which I want to replace
with " "
>
> gsub(".", " ", file.label) replaces the every character
with a blank.
>
> However gsub(".xls", " ", file.label) replaces
".xls" with a blank as
> expected.
>
> It appears that "." is some kind of wild-card. How do I tell
gsub that a
> period is just a period?
>
> Thanks.
>
>
> Charles Annis, P.E.
>
> Charles.Annis at StatisticalEngineering.com
> phone: 561-352-9699
> eFax: 614-455-3265
> http://www.StatisticalEngineering.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.
>