Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace two dots in a string with gsub

Tags:

r

I'm trying to use the following code to replace two dots for only one:

test<-"test..1"
gsub("\\..", ".", test, fixed=TRUE)

and getting:

[1] "test..1"

I tried several combinations of escape strings, including brackets [] with no success.
What am I doing wrong?

like image 208
FAC Avatar asked Aug 30 '13 02:08

FAC


1 Answers

If you are going to use fixed = TRUE, use the (non-interpreted) character .:

> gsub("..", ".", test, fixed = TRUE)

Otherwise, within regular expressions (fixed = FALSE), . has a special meaning (any character) so you'll want to prefix it with a backslash to mean "the dot character":

> gsub("\\.\\.", ".", test)
> gsub("\\.{2}", ".", test)
like image 174
flodel Avatar answered Nov 03 '22 15:11

flodel