R - gsub replacing backslashes

余生长醉 提交于 2019-11-27 04:49:12

Here's what you need:

gsub("\\\\", "\\\\\\\\", "\\")
[1] "\\\\"

The reason that you need four backslashes to represent one literal backslash is that "\" is an escape character in both R strings and for the regex engine to which you're ultimately passing your patterns. If you were talking directly to the regex engine, you'd use "\\" to indicate a literal backslash. But in order to get R to pass "\\" on to the regex engine, you need to type "\\\\".


(If you are just wanting to double backslashes, you might want to use this instead):

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