'Replace "%" with "\%" in string
I need to make a character vector suitable for use in LaTeX, so I need to escape all the percent signs with a single backslash. So far, all I've been able to do is to get two backslash:
> text <- c("This is 60%", "This is 30%")
> gsub("%", "\\%", text, fixed=TRUE)
[1] "This is 60\\%" "This is 30\\%"
> gsub("%", "\\\\%", text, fixed=TRUE)
[1] "This is 60\\\\%" "This is 30\\\\%"
As expected, if fixed=FALSE I get no backslash using "\\%" as a substitution or two backslash using `"\\%"' as a substitution. How can I get "This is 60\%" as a result?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
