remove all whitespaces from a string in R

R – Remove Whitespaces From String

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?

remove all 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 the pattern).
  • 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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top