To remove unnecessary spaces, use the str_squish function. The str_squish function is included in the stringr package of the R programming language and performs the function of removing leading, trailing, and intervening whitespace from a string. To remove unnecessary spaces, let's check the concept and main uses of the str_squish function.
Original Korean article: str_squish function to remove unnecessary spaces
1. The concept of str_squish
str_squish() removes unnecessary spaces at the beginning and end of the target string, and reduces consecutive spaces within the string to a single space. For example, if you have a string called “Hello World”, applying the str_squish() function will convert it to “Hello World”.
# stringr example example
library(stringr)
# example
str_squish(" Hello World ")
# example: "Hello World"
2. Main usage
Basic usage
- str_squish(string)
string: target string
1) Apply to vector
The str_squish function can also be applied to string vectors. In this case, the function is applied to each string element.
# example
str_squish(c(" Hello ", " World "))
# example: "Hello" "World"
2) Apply to data frame
The str_squish function with the dplyr package allows you to apply a function to specific columns in a data frame.
# dplyr example example
library(dplyr)
# example example example
df <- data.frame(name = c(" Alice ", " Bob ", " Carol "),
age = c(30, 40, 50))
# str_squish example
df <- df %>%
mutate(name = str_squish(name))
# example example
print(df)
3. Conclusion
The str_squish() function is a very useful tool when squeezing text data. This function simplifies complex string processing tasks. It is often used in data analysis or text mining tasks, so it is a good idea to learn how to use this function.
To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).
View all R programs
Good article to read together
- 1. What is research? [R Statistics]
- Text replacement str_replace, str_replace_all functions
- Understanding Tibble and the as_tibble() function
- unnest_tokens() function
- Execute PHP and R code in conjunction
Practical Use Cases for str_squish()
In real text analysis projects, unnecessary spaces often appear when data is copied from web pages, spreadsheets, survey answers, PDF extractions, or manually typed forms. A string may look clean on the screen, but it can contain repeated spaces, tabs, or line-break-like spacing that makes grouping, joining, filtering, or tokenizing less reliable.
The value of str_squish() is that it solves two problems at once. First, it removes leading and trailing whitespace. Second, it compresses repeated internal whitespace into a single normal space. This makes the function especially useful before comparing labels, cleaning names, standardizing categories, or preparing text for tokenization.
When to Use It in an R Workflow
- Before using text as a key for joins or matching.
- Before counting unique values in a survey or category column.
- Before tokenizing Korean or English text for text mining.
- Before exporting cleaned data to a report or dashboard.
A practical rule is simple: if a text column came from outside your own controlled data pipeline, normalize whitespace early. That small cleaning step can prevent confusing duplicate categories and unexpected mismatches later in the analysis.
Related Reading
- Related Thinknote article
- Related Thinknote article
- Related Thinknote article
- Related Thinknote article
- Related Thinknote article
FAQ
What is this article about?
This article is an English translation and global-reader adaptation of the Korean post “str_squish function to remove unnecessary spaces.” It preserves the original article’s main explanation, examples, and practical context.
Why is it translated into English?
The English version helps global readers access Thinknote articles through English search keywords while keeping the Korean source available as the original reference.
Where can I read the original Korean version?
You can read the original Korean article here: https://www.thinknote.co.kr/r-str-squish-remove-spaces/