The text replacement (str_replace, str_replace_all) function is a very useful tool in string processing. The text replacement function is included in the stringr package and replaces one string with another.
Original Korean article: Text replacement str_replace, str_replace_all functions
1. Concept of text replacement (str_replace, str_replace_all) function
1) str_replace()
The str_replace() function replaces the first occurrence of a specific pattern within a string with another string.
# stringr example example
library(stringr)
# example
str_replace("apple orange apple", "apple", "banana")
# example: "banana orange apple"
In the example above, “apple” was replaced with “banana” only on its first occurrence.
2) str_replace_all()
On the other hand, the str_replace_all() function replaces every specific pattern within a string with another string.
# example
str_replace_all("apple orange apple", "apple", "banana")
# example: "banana orange banana"
In the example above, all “apple” words have been replaced with “banana”.
2. Main usage of str_replace, str_replace_all functions
Basic usage
- str_replace(string, pattern, replacement)
- str_replace_all(string, pattern, replacement)
string: target string pattern: pattern to find replacement: string to replace
1) Pattern matching using regular expressions
You can use regular expressions in the pattern parameter. For example, if you want to remove all numbers, you can do this:
# str_replace() example example
str_replace("apple1 orange2", "[0-9]", "")
# example: "apple orange2"
# str_replace_all() example example
str_replace_all("apple1 orange2", "[0-9]", "")
# example: "apple orange"
2) Replace multiple patterns at once
The str_replace_all() function can replace multiple patterns at once. In this case, we pass pattern and replacement as named vectors.
# example example example example
str_replace_all("apple orange pear", c("apple" = "banana", "orange" = "grape"))
# example: "banana grape pear"
3) Replace all characters except Korean, English, and numbers with empty data
You can use the str_replace_all function to replace all characters except Korean, English, and numbers with empty data. Let's apply this using regular expressions.
Below is an example using the stringr package.
# stringr example example
library(stringr)
# example example
example_str <- "example! Hello, 1234!!@@"
# example, example, example example example example example example example
cleaned_str <- str_replace_all(example_str, "[^example-examplea-zA-Z0-9]", "")
# example example
print(cleaned_str)
#exampleHello1234
In the above code, "[^ga-hia-zA-Z0-9]" means all characters except Korean (ga-hi), English (a-zA-Z), and numbers (0-9). You can replace these with an empty string and get the result:
3. Concluding how to use the text replacement function
The text replacement (str_replace, str_replace_all) function is a very useful tool when processing text data. Using these functions, you can easily solve complex string processing tasks.
In particular, when used with regular expressions, you can achieve more powerful string processing capabilities.
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
- Install PHP 8 (ubuntu)
- Setting up Nginx + Php8
- Install memory caching APCu, Redis, Memcached
- Install Centos 8
- Linux user management useradd usermod userdel
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 “Text replacement str_replace, str_replace_all functions.” 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-replace-functions/