• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

gsub() in R to replace all matching patterns

rstats101 · December 22, 2021 ·

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"

Related

Filed Under: gsub Tagged With: remove backslash with gsub in R, remove empty space with gsub in R

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version