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" |
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") |
install.packages("stringr") # Install & load stringr package library("stringr")
str_squish(my_string) # Apply str_squish function # [1] "aa b ccc d" |
str_squish(my_string) # Apply str_squish function # [1] "aa b ccc d"