Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a backslash in R? [duplicate]

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?

like image 537
Paul Fournel Avatar asked Feb 14 '13 16:02

Paul Fournel


People also ask

How do you escape a backslash in R?

The following R code explains how to escape a backslash in a character string. To accomplish this, we have to specify a second backslash in front of the backslash that we want to escape.

What is a backslash escape character?

Escape character syntaxAn escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0-9, A-F, a-f).


2 Answers

I found a solution that works

str = gsub("([\\])","", str) 
like image 162
Paul Fournel Avatar answered Sep 24 '22 16:09

Paul Fournel


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

like image 25
Ramnath Avatar answered Sep 23 '22 16:09

Ramnath