Remove All Whitespace in Each Data Frame Column in R (2 Examples)


In this tutorial, I’ll show how to delete all blanks in data frame variables in the R programming language.

The content of the article looks like this:

Let’s dive right into the programming part!


Introducing Example Data

We use the following data as basement for this R tutorial:

data <- data.frame(x1 = c("a a", "  b", "c c c "),  # Create example data
                   x2 = c("x x x", "y     ", "z"))
data                                                # Print example data


table 1 data frame remove all whitespace data frame r


Have a look at the table that has been returned by the previous syntax. It shows that our exemplifying data consists of three rows and two columns. Both variables have the character class and contain blanks.


Example 1: Delete All Blanks in Data Frame Using gsub() Function

The following R code illustrates how to use the apply function in combination with the gsub function to remove all whitespace from each data frame column.

Have a look at the R code below:

data_new1 <-,              # Remove blanks
                                 function(x) gsub("\\s+", "", x)))
data_new1                                           # Print updated data


table 2 data frame remove all whitespace data frame r


After executing the previous code the data frame you can see in Table 2 has been created. As you can see, we have removed all blanks from our data frame.

Note that we have used the function to retain the data.frame class. Otherwise, our data would have been converted to a matrix object.


Example 2: Delete All Blanks in Data Frame Using str_remove_all() Function of stringr Package

In this example, I’ll show how to use the stringr package to extract all spaces in our data frame.

If we want to use the functions and commands of the stringr package, we first need to install and load stringr:

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

Now, we can use the apply and str_remove_all functions to delete all whitespace.

data_new2 <- apply(data, 2, str_remove_all, " ")    # Remove blanks
data_new2                                           # Print updated data


table 3 matrix remove all whitespace data frame r


In Table 3 it is shown that we have deleted all blanks with the previous R code.

This time we have not used the function. For that reason, our output data is a matrix object.


Video, Further Resources & Summary

If you need more info on the R codes of this article, I recommend watching the following video of my YouTube channel. In the video, I’m explaining the R programming syntax of this tutorial:



In addition, you might want to read some of the other articles on this website. A selection of articles about data frames is shown below.


In summary: You have learned in this article how to remove spaces in all columns of a data frame in the R programming language.

We have trimmed all leading and trailing whitespace, and we have removed all blanks in the middle of each string.

Tell me about it in the comments section, in case you have further questions.


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.