I am working on some raw text and want to replace all multiple spaces with one space. Normally, would use stringr's str_squish
, but unfortunately it also removes linebreaks (\n and \r) which I have to keep.
Any idea? Below my attempts. Many thanks!
library(tidyverse)
x <- "hello \n\r how are you \n\r all good?"
str_squish(x)
#> [1] "hello how are you all good?"
str_replace_all(x, "[:space:]+", " ")
#> [1] "hello how are you all good?"
str_replace_all(x, "\\s+", " ")
#> [1] "hello how are you all good?"
Created on 2020-07-01 by the reprex package (v0.3.0)