In this tutorial, we will look at how to remove all the whitespaces from a string in R with the help of some examples.
How to remove whitespaces from a string in R?

You can use the gsub()
function in R to remove whitespaces from a string. The gsub()
is used for replacing characters in a string. The following is the syntax –
# remove spaces from string s gsub(" ", "", s)
Let’s look at the key arguments to the above function in detail –
pattern
– The pattern (or string) to replace. You can also pass a regular expression as a pattern.replacement
– The string replacement to replace the matched string (as defined by thepattern
).x
– The string or string vector to operate on.
To remove whitespaces, use the whitespace character " "
as the pattern
and empty string ""
as the replacement
.
Note that the gsub()
function does not modify the string, rather it returns a new string with every instance of pattern
replaced by the replacement
.
Examples
Let’s now look at some examples of removing whitespaces from a string.
Remove whitespaces using gsub()
function
Pass a single space, " "
as the string to be replaced. An empty string, ""
as the replacement string and the string variable s
to the gsub()
function.
# create string s <- "have a nice day!" # remove spaces from string new_s <- gsub(" ", "", s) # display the resulting string print(new_s)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[1] "haveaniceday!"
You can see that the resulting string does not have any spaces.
Let’s look at another example. This time we’ll use a string with inconsistent spaces.
# create string s <- "have a nice day!" # remove spaces from string new_s <- gsub(" ", "", s) # display the resulting string print(new_s)
Output:
[1] "haveaniceday!"
You can see that all the occurrences of white space have been removed (replaced by an empty string) in the string above.
Remove whitespaces using str_replace_all()
function
Alternatively, you can also use the str_replace_all()
function available in the stringr
library to remove whitespaces from a string. This similar to the gsub()
function just be mindful of the syntax.
The following is the syntax to remove whitespaces from a string using str_replace_all()
function.
str_replace_all(x, pattern, replacement)
Note that the stringr
library must be imported in order to use this function.
Let’s look at an example. We’ll use the same string from the example above.
# load the package library("stringr") # create string s <- "have a nice day!" # remove spaces from string new_s <- str_replace_all(s, " ", "") # display the resulting string print(new_s)
Output:
[1] "haveaniceday!"
You can see that the resulting string does not contain any whitespaces.
You might also be interested in –
- Remove Duplicates From a Vector in R
- R – Remove Element From a Vector
- Combine Two Vectors Into a Single Vector in R
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.