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
.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
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:
[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.