R: How to replace . in a string?

前端 未结 4 688
旧巷少年郎
旧巷少年郎 2020-12-11 14:33

I have a string say \"a.b\" and I want to replace \".\" with \"_\".

gsub(\".\",\"_\",\"a.b\")

doesn\'t work as . matches all characters.

4条回答
  •  被撕碎了的回忆
    2020-12-11 15:22

    . matches any character. Escape . using \ to match . literally.

    \ itself is also should be escaped:

    > gsub("\\.", "_", "a.b")
    [1] "a_b"
    

提交回复
热议问题