0

This is the real value of a cell in Dataframe CSV :

"window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf', '_blank')"

after extract this value in R I get additional slash ( \ ) before each ( " )as shown below:
"window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf', '_blank')"

How I could fix this problem, I have changed the encoding to "UTF-8" but still, nothing happened!

  • Could you elaborate what's the actual nature of the problem you are trying to solver? Is the content of `info_360$more` inconsistent with your expectations? If so could you edit your question including sample data in machine readable format, the desired outcome and transformation you are applying. Please have a look at the [*How to make a great R reproducible example*](https://stackoverflow.com/q/5963269/1655567) discussion in order to improve your question. – Konrad Mar 02 '22 at 21:12
  • The value of info_360$more changes automatically by adding slash before each ( " ), which then make the web link not working any more, I hope its clear now – Mohamed Osman Mar 02 '22 at 21:19
  • So it's actually about writing out the string without inverted commas. Say, in the text file you would get `window.open...` and now the file content is `"window.open...` and presence of `"` is an issue? – Konrad Mar 02 '22 at 22:43
  • no I want to get this ( "window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf', '_blank')" ) instead I get this ( \"window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf', '_blank') \" ) with slashs – Mohamed Osman Mar 02 '22 at 22:48

1 Answers1

0

Short answer:

gsub("\"", "", info360$more)

Explanation:

The backslashes are escape characters for the quotes ("). These need to be escaped, as a characters in R are enclosed in quotes. Try executing cat(info360$more). You'll see that the backslashes disappear, and the one pair of quotes remains. So you want to get rid of the extra pair of quotes, which will also make the backslashes disappear.

Here is a little example. Try to understand why the output of a and cat(a) is different. That should help you to solve your issue.

a <- c("\"test\"", '"test2"', "test3")
a
#> [1] "\"test\""  "\"test2\"" "test3"
cat(a)
#> "test" "test2" test3

Hint: The output of cat(a) displays the "actual" value of a, while a produces output that is "decorated" with escape characters, in order to make the output compatible with R code.

The extra pair of quotes most likely is coming from the original data in the CSV file. Can you post the raw CSV in your question? Open it in a text editor, not excel for that purpose.

Till
  • 3,845
  • 1
  • 11
  • 18