-6

I read a HTML file into R and remove all HTML files based on previous answers. However, there is always \r\n inside the text. How can I replace them with " "? Your inputs will be appreciated. Thanks.

lucyh
  • 179
  • 2
  • 5
  • 14
  • Please provide a small, reproducible example with sample input data and expected output that closely resembles your problem. – MrFlick May 07 '14 at 20:55

2 Answers2

3

Simple:

> x <- 'abc\r\n123\r\n'
> gsub('[\n\r]',' ',x)
[1] "abc 123 "
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Maybe `gsub('[\n\r]',' ',x)` might be better? Or `gsub('\r\n', ' ', x)` – Rich Scriven May 07 '14 at 19:53
  • @RichardScriven Yes, definitely. I wrote this answer without testing it. – Thomas May 07 '14 at 19:56
  • My file is data.frame. The gsub did not work. Thanks. – lucyh May 07 '14 at 19:57
  • 4
    @lucyh Then please read about how to supply [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas May 07 '14 at 19:59
  • None of these seems to work: Any suggestions? `> x <- "\\nDécor is fun" > gsub('[\\n]',' ',x) [1] " Décor is fu " > gsub('[\n\r]',' ',x) [1] "\\nDécor is fun"` – torenunez Apr 02 '20 at 23:44
  • Ended up using a hacky solution in this post: https://stackoverflow.com/questions/51384784/how-to-replace-r-n-characters-in-a-text-string-specifically-in-r `> x <- "\\nDécor is fun" > gsub('__n', '', gsub('[\\\\]', '__', x)) [1] "Décor is fun"` – torenunez Apr 02 '20 at 23:56
0

None of these worked for me; I had to use:

> x <- 'abc\r\n123\r\n'
> gsub('[\r\n][\r\n]',' ',x,perl=TRUE)
[1] "abc 123 "
R_User123456789
  • 650
  • 7
  • 9