0

Like the title says, I am trying to replace plus-minus signs with the equivalent LaTeX command "\pm" using R

I have tried the following with no success:

gsub("±", "\\pm", "±", fixed = FALSE)

"pm"

gsub("\u00b1", "\\pm", "\u00b1", fixed = FALSE)

"pm"

What am I doing wrong?

d.b
  • 32,245
  • 6
  • 36
  • 77
jsta
  • 3,216
  • 25
  • 35

1 Answers1

0

You needed to do it once more. You need four backslash to represent one (Read more here).

gsub("±", "\\\\pm", "±")
#[1] "\\pm"

It is actually only one backslash. Check by running:

cat(gsub("±", "\\\\pm", "±"))
#\pm
Community
  • 1
  • 1
d.b
  • 32,245
  • 6
  • 36
  • 77
  • If you used `fixed = TRUE`, you can skip escaping them for regex purposes: `sub("±", "\\pm", "±", fixed = TRUE)` – alistaire Apr 11 '17 at 17:03