gsub() in R to replace all matching patterns

In this tutorial, we will learn about gsub() function in R to replace all matching patterns in a string.

gsub, stands for “global substitution” is similar to another function R, sub(), but for substituting all matching patterns, not just the first one. Here is a typical use of gsub() in R. We need to provide the pattern to be replace, and what we want to replace with and then the string that we need to make the changes.

gsub(pattern_to_be_replaced, replace_with, in_this_string)

We will see multiple examples on how to use gsub() in R. To start with we will replace all white spaces with underscore. Then we will see an example of replacing one string with another. And then we will learn how to use gsub() to replace special characters like “?”, “.”, and “/”.

Substitute white space with underscore using gsub in R

Let us start with replacing all white spaces with underscore using gsub() function in R. Here is our text/string needs fixing

my_str1 <- "replace all the spaces with underscore using gsub"

We replace all white spaces with under score using gsub(). Note every white space is replaced with underscore.

gsub(" ","_",my_str1)
"replace_all_the_spaces_with_underscore_using_gsub"

Here is another example, where we replace a substring in a text.

my_str <- "gsub() is a function in R. Learn how to use gsub() to replace string in R"
gsub("R","R programming language",my_str)
1] "gsub() is a function in R programming language. Learn how to use gsub() to replace string in R programming language"

How to replace question mark with gsub() in R

To replace special characters with something else in R using gsub, we need to specify that it is a special character.

my_str2 <- "how to remove question mark ? with gsub"

For example to replace a question mark we need to use “\\” two backslashes before “?”. In this example we replace a question mark symbol with nothing “”.

gsub("\\?", "", my_str2)  
"how to remove special character, the question mark  with gsub"

How to replace dots “….”, with gsub() in R

Similarly, if we want replace dot “.”, we need to specify “\\” backslashes before the “.”

my_str2 <- "how to remove dot . with gsub"
gsub("\\.", "", my_str2)  
"how to remove dot  with gsub"
my_str <- "how to remove dots .... with gsub"
gsub("\\.", "", my_str)  ##
"how to remove dots  with gsub"

How to replace backslash with gsub() in R

my_str <- "how to back slash \ with gsub"
#gsub("\\\", "", my_str)  ##
Error: Incomplete expression: gsub("\\\", "", my_str) 
my_str <- "how to remove backslash \ with gsub"
gsub("\\\\", "", my_str)  ##
"how to remove backslash  with gsub"
Exit mobile version