How to replace a series of whitespace with just one in a string in R?

Asked

Viewed 503 times

3

How do I replace a series of blank spaces 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: "non non non non non non non non".

  • 2

    I have no R skills, but the regular expression for finding the spaces would be \s*, then just replace by a space.

  • Thank you, I found the solution!

1 answer

4


Two possible solutions:

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

and

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

    Spoke a + in the second solution gsub("[[:space:]]+", " ", x)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.