Replace Multiple Spaces by Single Space in R (2 Examples)


This article illustrates how to remove redundant spaces from a character string in the R programming language.

The post is structured as follows:

Let’s jump right to the examples…


Creation of Example Data

The first step is to create some data that we can use in the following examples:

x <- "   This string   contains  many   spaces "        # Example string
x                                                       # Print example string
# [1] "   This string   contains  many   spaces "

As you can see based on the previous output of the RStudio console, our example data is a character string containing several double spaces. Furthermore, our example string contains leading and trailing white spaces.


Example 1: Remove Double Spaces Using gsub() Function

Example 1 illustrates how to use the gsub function provided by the basic installation of the R programming language to get rid of all double spaces and all leading and trailing spaces in a character string.

Have a look at the following R code:

gsub("(?<=[\s])\s*|^\s+|\s+$", "", x, perl = TRUE)  # Remove spaces
# [1] "This string contains many spaces"

As you can see based on the previous output of the RStudio console, we have converted multiple spaces to single spaces, and we have dropped leading and trailing whitespace.


Example 2: Remove Double Spaces Using str_squish() Function of stringr Package

As you have seen in Example 1, the R programming syntax when using the gsub function is relatively complex.

Fortunately, the stringr package provides an easier solution in case we want to merge multiple spaces into one space.

First, we have to install and load the stringr package:

install.packages("stringr")                             # Install stringr package
library("stringr")                                      # Load stringr package

Next, we can apply the str_squish function to trim leading and trailing whitespace, and to convert multiple spaces into one space:

str_squish(x)                                           # Remove spaces
# [1] "This string contains many spaces"

The previous output is exactly the same as in Example 1. However, this time the R code was much simpler thanks to the str_squish function.


Video, Further Resources & Summary

Would you like to learn more about the removal of redundant spaces from a character string? Then I can recommend having a look at the following video on my YouTube channel. In the video, I’m explaining the R programming syntax of this tutorial in a live session.



Furthermore, you might have a look at the other articles on my website. A selection of articles is listed below.


To summarize: In this R tutorial you have learned how to remove and replace double spaces from a character string. Don’t hesitate to let me know in the comments, in case you have further questions. Furthermore, please subscribe to my email newsletter to get regular updates on new articles.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.