Escape characters cause a lot of trouble in R, as evidenced by previous questions:
- Change the values in a column
- Can R paste() output "\"?
- Replacing escaped double quotes by double quotes in R
- How to gsub('%', '\%', ... in R?
Many of these previous questions could be simplified to special cases of "How can I get \ out of my way?"
Is there a simple way to do this?
For example, I can find no arguments to gsub
that will remove all escapes from the following:
test <- c('\01', '\\001')
The difficulty here is that "\1"
, although it's printed with two glyphs, is actually, in R's view a single character. And in fact, it's the very same character as "\001"
and "\01"
:
nchar("\1")
# [1] 1
nchar("\001")
# [1] 1
identical("\1", "\001")
# [1] TRUE
So, you can in general remove all backslashes with something like this:
(test <- c("\\hi\\", "\n", "\t", "\\1", "\1", "\01", "\001"))
# [1] "\\hi\\" "\n" "\t" "\\1" "\001" "\001" "\001"
eval(parse(text=gsub("\\", "", deparse(test), fixed=TRUE)))
# [1] "hi" "n" "t" "1" "001" "001" "001"
But, as you can see, "\1"
, "\01"
, and \001"
will all be rendered as 001
, (since to R they are all just different names for "\001"
).
EDIT: For more on the use of "\"
in escape sequences, and on the great variety of characters that can be represented using them (including the disallowed nul string mentioned by Joshua Ulrich in a comment above), see this section of the R language definition.
I just faced the same issue - if you want any \x where x is a character then I am not sure how, I wish I knew, but to fix it for a specific escape sequence,. say \n then you can do
new = gsub("\n","",old,fixed=T)
in my case, I only had \n