How to replace a series of blanks by just one in a string in R?

Posted on

Question :

How do I replace a series of blanks with just one, in R?

For example, suppose we have a string:

x <- "      non    non    non    non"

I want to make the content of x be “non non non non”.


Answer :

Two possible solutions:

gsub("s+", " ", x)


gsub("[[:space:]]+", " ", x)


Leave a Reply

Your email address will not be published. Required fields are marked *