Remove Redundant Spaces from Character String in R (Example Code)

In this R programming tutorial you’ll learn how to remove redundant spaces from a character string.

Creation of Example Data

my_string <- " aa   b  ccc   d"    # Create character string in R
my_string                          # Display example string in RStudio console
# [1] " aa   b  ccc   d"

Example: Converting Double Spaces to Single Space Using stringr Package

install.packages("stringr")        # Install & load stringr package
library("stringr")
str_squish(my_string)              # Apply str_squish function
# [1] "aa b ccc d"

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top