How to escape a backslash in R? [duplicate]

孤街浪徒 提交于 2019-11-26 08:24:41

问题


This question already has an answer here:

  • How to escape backslashes in R string 3 answers

I\'m working in R and having troubles escaping the backslash. I am using the library stringr.

install.packages(\"stringr\", repos=\'http://cran.us.r-project.org\')
library(\"stringr\")

I would like to do str = str_replace_all(str, \"\\\", \"\")

So I tried str = str_replace_all(str, \"\\\\\", \"\") but it won\'t work.

What should I do?


回答1:


I found a solution that works

str = gsub("([\\])","", str)



回答2:


Use Hmisc::escapeRegex and Hmisc::escapeBS which automatically escapes backslashes and other regex special characters.



来源:https://stackoverflow.com/questions/14879204/how-to-escape-a-backslash-in-r

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