-1

How can one use regex to remove + signs in strings.

I am sure there is a simple solution but I couldn't find it. Here is a toy example.

X            Y

mouse+       8 
elephant+    9
wolf         5

Attempted many variations of gsub but with no success.

DF$X <- gsub(DF$X, pattern = "\+", replacement = "")

Output should look like this

X            Y

mouse        8 
elephant     9
wolf         5
Krutik
  • 461
  • 4
  • 13
  • 1
    `DF$X <- gsub(DF$X, pattern = "+", replacement = "",fixed=T)` – Duck Aug 12 '20 at 15:41
  • Very quick, thank you. Knew it would be a simple fix. – Krutik Aug 12 '20 at 15:42
  • 1
    The `"+"` symbol has special meaning in regular expression, so if you need the exact "+" symbol, you have to use an escape character, i.e. `"\\+"`(double backslash) or set `fixed = T`. – Darren Tsai Aug 12 '20 at 16:18

1 Answers1

0

Another way you can do

library(stringr)
df <- df %>% 
  mutate(X = str_replace_all(X, "(\\+)", ""))
#         X  Y
# 1    mouse 8
# 2 elephant 9
# 3     wolf 5
Tho Vu
  • 1,304
  • 2
  • 8
  • 20