0

I have this text and I want to replace // with \

This is the text sdfd//dfsadfs

and I want it to be sdfd\dfsadfs

Can gsub work? This does not work: gsub("//","[\]","sdfd//dfsadfs")

joel.wilson
  • 8,243
  • 5
  • 28
  • 48
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • 4
    `"sdfd\dfsadfs"` is an invalid string in r. You can get `"sdfd\\dfsadfs"` with `sub("//","\\\\","sdfd//dfsadfs")` – Psidom Dec 23 '16 at 16:52

1 Answers1

2

I had a similar problem before. Like @Psidom commented, you should use gsub("//","\\\\","sdfd//dfsadfs"). This will replace //(2 characters) with \\ which is actually a single character in R (Check by running nchar("\\")). Even though it is prints as \\, it behaves as \. You can check this by running cat("\\"). If you exported the data after running gsub to a table (or csv), I believe there will be only one \

d.b
  • 32,245
  • 6
  • 36
  • 77