Skip to content

gsub difficulty

3 messages · Charles Annis, P.E., Marc Schwartz, Gabor Grothendieck

#
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:
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
#
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: