Replacing \r\n in a dataframe

I have a dataframe (combined from many HTML tables) that has an address column. Some values in this column include line breaks (\r\n), but no matter what I've tried with gsub {gsub("[\r\n]", " ", tabletest)} or str_replace {(tr_replace_all(tabletest, "\r\n" , " ")} it's still returning strings with \r\n in tabletest.

The column is char values, I've tried running it with as.matrix {str_replace_all(as.matrix(tabletest), "(\r\n)" , " ")}

Reprex coming shortly.

Try double back slashes.

Df <- data.frame(TEXT = c("Here is \r\n some text with \r\n line breaks"))
#>                                           TEXT
#> 1 Here is \r\n some text with \r\n line breaks
Df$TEXT2 <- gsub("\\r\\n", "", Df$TEXT)
#>                                           TEXT
#> 1 Here is \r\n some text with \r\n line breaks
#>                                  TEXT2
#> 1 Here is  some text with  line breaks

Created on 2019-12-02 by the reprex package (v0.3.0.9000)

You could also use the whitespace character class:

gsub("\\s+", " ", tabletest)

This matches one or more whitespaces in a row and replaces them with a single and normal space. If you don't want to preserve tabs, this is usually a good cleaner.

I shall try that!

The text in question doesn't have whitespace around the breaks when I check it in the dataframe, eg "Faketown\r\nUnusual street..."

Is that an issue? Have tried "\r\n", but will give the doubles slashes a go!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.