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
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 <- as.data.frame(apply(data, # Remove blanks 2, function(x) gsub("\\s+", "", x))) data_new1 # Print updated data
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 as.data.frame 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
In Table 3 it is shown that we have deleted all blanks with the previous R code.
This time we have not used the as.data.frame 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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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.
- Remove Data Frame Columns by Name in R
- Remove First Row of Data Frame in R
- Remove All-NA Columns from Data Frame
- Remove All White Space from Character String in R
- The R Programming Language
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.
Statistics Globe Newsletter