1

I have look into the web and found this webpage In R, replace text within a string to replace a text within in a string.

I tried the same method to replace the punctuation "." into another punctuation "-" but it did not work.

 group <- c("12357.", "12575.", "197.18", ".18947")
 gsub(".", "-", group)

gives this output

 [1] "------" "------" "------" "------"

instead of

 [1] "12357-" "12575-" "197-18" "-18947"

Is there an alternate way to do this ?

Community
  • 1
  • 1
JauntyJJ
  • 47
  • 5

1 Answers1

7

"." in regex langage means "any character". To capture the actual point, you need to escape it, so:

gsub("\\.", "-", group)
#[1] "12357-" "12575-" "197-18" "-18947"

As mentioned by @akrun in the comments, if you prefer, you can also enclosed it in between brackets, then you don't need to escape it:

gsub('[.]', '-', group)
[1] "12357-" "12575-" "197-18" "-18947"
Cath
  • 23,906
  • 5
  • 52
  • 86