Suppose I have a character vector like
"Hi, this is a good time to start working together.".
I just want to have
" Hi, this is a good time to start working together."
Only one white space between two words. How should I do this in R?
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
trimws() function in R Language is used to trim the leading white spaces. It shrinks an object by removing outermost rows and columns with the same values.
gsub
is your friend:
test <- "Hi, this is a good time to start working together." gsub("\\s+"," ",test) #[1] "Hi, this is a good time to start working together."
\\s+
will match any space character (space, tab etc), or repeats of space characters, and will replace it with a single space " "
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With