How to give Backslash as replacement in R string replace [duplicate]

笑着哭i 提交于 2019-12-11 05:52:51

问题


I need to ">" with "\". Example : "a>b" should be changed to "a\b"

I have tried gsub

> test <- "a>b"
> gsub(">","\\",test, fixed = TRUE)
[1] "a\\b"

I have tried StringR str_replace

> library(stringr)
> str_replace(test,">","\\")
[1] "ab"

I have tried Stringi str_replace_all_fixed

> library(stringi)
> stri_replace_all_fixed(test,">","\\")
[1] "a\\b"

How do I escape \ in replacement. I guess when you give \, it is expecting \1, \2 etc for captured groups. How to avoid this

来源:https://stackoverflow.com/questions/46239045/how-to-give-backslash-as-replacement-in-r-string-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!