With a regular expression and the function gsub():

group <- c("12357e", "12575e", "197e18", "e18947")
group
[1] "12357e" "12575e" "197e18" "e18947"

gsub("e", "", group)
[1] "12357" "12575" "19718" "18947"

What gsub does here is to replace each occurrence of "e" with an empty string "".


See ?regexp or gsub for more help.

Answer from Andrie on Stack Overflow
🌐
Tidyverse
stringr.tidyverse.org › reference › str_replace.html
Replace matches with new text — str_replace • stringr
Source: R/replace.R · str_replace.Rd · str_replace() replaces the first match; str_replace_all() replaces all matches. str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement) string · Input vector.
🌐
RDocumentation
rdocumentation.org › packages › stringr › versions › 0.3 › topics › str_replace
str_replace function - RDocumentation
Learn R Programming · stringr (version 0.3) Replace replaced occurences of a matched pattern in a string. str_replace(string, pattern, replacement) string · input character vector · pattern · pattern to look for, as defined by a POSIX regular expression. See the ``Extended Regular Expressions'' ...
Discussions

Str_replace_all - how to use
I am practising some R skills on some dummy data. I want to replace all specific values in a very large data set with other values. So for example I want to replace ALL of the instances of "Long Hair" with a blank character cell as such " ". Sounds nuts but there is a point to it! I tried using the following... df1 %>% str... More on forum.posit.co
🌐 forum.posit.co
1
0
August 21, 2018
Str_replace Regex in R - Stack Overflow
I am trying to figure out what would be the best way to remove the parenthesis and everything inside of it using str_detect and regex. I've found responses that assist in removing parenthesis with ... More on stackoverflow.com
🌐 stackoverflow.com
regex - Trying to replace a () in a string in R using str_replace - Stack Overflow
I am trying to replace a () in a string using the sub_string function in R but it appears that due that the function is overlooking the (). I am pretty new to coding and R so I imagine that it has More on stackoverflow.com
🌐 stackoverflow.com
Replace String - Looping Over Vector Help
Instead of x = i it should be x = years[i] Also, you don't need to use a for loop here. Apply gsub to the years vector instead. More on reddit.com
🌐 r/Rlanguage
7
1
September 15, 2020
🌐
Statology
statology.org › home › how to use str_replace in r (with examples)
How to Use str_replace in R (With Examples)
June 27, 2022 - The str_replace() function from the stringr package in R can be used to replace matched patterns in a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › r language › how-to-use-str_replace-in-r
How to Use str_replace in R? - GeeksforGeeks
December 2, 2021 - Data Viz. using R ... str_replace() is used to replace the given string with a particular value in R Programming Language.
Top answer
1 of 3
2

You can use str_replace_all and add another \ when escaping \ in the regex.

library(tidyverse)
Example %>% mutate(Column1 =  str_replace_all(Column1, "\\([^()]*\\)", ""))
#          Column1
#1      Pineapple 
#2           Roger
#3                
#4 First , Second 
2 of 3
1

It's not clear to me how you want to deal with entries where you have more than one number. That aside and generally, a more convenient option is to use readr::parse_number, rather than using stringr::str_detect/stringr::str_remove. parse_number takes care of additional text, units and thousands separators.

If you want to keep only the first number (in the cases where there are more than one number per entry), you can do

library(tidyverse)
Example %>% mutate(Column1 = parse_number(Column1))
#  Column1
#1    1000
#2   50000
#3    1000
#4     100

Or if you want to keep both/multiple numbers, I suggest using separate_rows to separate entries based on a comma followed by a whitespace, before using readr::parse_number.

Example %>%
    separate_rows(Column1, sep = ",\\s") %>%
    mutate(Column1 = parse_number(Column1))
## A tibble: 5 × 1
#  Column1
#    <dbl>
#1    1000
#2   50000
#3    1000
#4     100
#5    1000

Update

To separate keys and values, here is an option; please see inline comments for explanations:

library(tidyverse)
Example %>%
    # Separate multiple comma-separated entries into rows
    separate_rows(Column1, sep = ",\\s") %>%
    # Swap "(value) key" > "key (value)" %>%
    mutate(Column1 = str_replace(
        Column1, "^(\\(.+\\))\\s(\\w+)$", "\\2 \\1")) %>%
    # Separate "key (value)" into columns
    separate(Column1, c("key", "value"), sep = "\\s", fill = "left") %>%
    # Parse number
    mutate(value = parse_number(value))
## A tibble: 5 × 2
#  key       value
#  <chr>     <dbl>
#1 Pineapple  1000
#2 Roger     50000
#3 NA         1000
#4 First       100
#5 Second     1000

Sample data

Example <- data.frame(Column1 = c(
    "Pineapple (50,000) Roger", 
    "($1,000)", 
    "First ($100), Second ($1,000)"))
🌐
R-statistics
r-statistics.co › home › stringr str_replace_all() in r: replace all pattern matches
stringr str_replace_all() in R: Replace All Pattern Matches
June 22, 2026 - The str_replace_all() function in stringr replaces EVERY match of a pattern in each input string. It accepts a single replacement, a named vector of patterns, or a function callback, making it the workhorse for bulk text substitution in R.
🌐
Stack Overflow
stackoverflow.com › questions › 40807580 › trying-to-replace-a-in-a-string-in-r-using-str-replace
regex - Trying to replace a () in a string in R using str_replace - Stack Overflow
Welcome to Stackoverflow, I would suggest you to read first how to ask questions on stackoverlfow before posting. I would also point you toward the following topic on how to replace regular expressions in R. ... Perhaps, you can just use a literal replacement: mutate(feature,feature=str_replace(feature$feature,fixed("()"),""))
🌐
Reddit
reddit.com › r/rlanguage › replace string - looping over vector help
r/Rlanguage on Reddit: Replace String - Looping Over Vector Help
September 15, 2020 -

Hello,

I am needing some help with replacing a character in each value in my vector. This is my vector.

years <- c("X1996","X1997","X1998","X1999","X2000","X2001","X2002","X2003","X2004","X2005","X2006","X2007","X2008","X2009","X2010","X2011","X2012","X2013","X2014","X2015")

What I am trying to do is replace the "X" in each item in the vector. When I do it individually I get the results I want:

y <- years[1]
y
#[1] "X1996"
y <- gsub(pattern = "X",replacement = "",x=y)
y
#[1] "1996"

When I loop over it, i just get the numbers 1:20:

for(i in 1:length(years)){
  years[i] <- gsub(pattern = "X",replacement = "",x=i)
}
years
#[1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"

What am I doing wrong? I have worked with vectors before but can't seem to figure out what is happening.

🌐
Posit Community
forum.posit.co › tidyverse
Replacing multiple patterns via str_replace - tidyverse - Posit Community
August 2, 2021 - Hi, I would like to identify multiple patterns in a character vector and replace each pattern with a specific replacement. For this, I extract each match, modify the letters in the way I need and would like to replace …
🌐
Tidyverse
stringr.tidyverse.org › articles › stringr.html
Introduction to stringr • stringr
These are parameterised by the ... vector of strings to process and a single pattern to match. stringr provides pattern matching functions to detect, locate, extract, match, replace, and split strings....
🌐
Analyticohub
analyticohub.com › 2020 › 03 › 21 › 2020-03-21-replace-strings-in-r-using-str-replace
Replace Strings in R using str_replace · Analytico Hub
## # A tibble: 6 x 4 ## id dateAdded dateUpdated address ## <chr> <dttm> <dttm> <chr> ## 1 AVwc252WIN2L1WUfpqLP 2016-10-30 21:42:42 2018-09-10 21:06:27 5921 Valencia Cir ## 2 AVwc252WIN2L1WUfpqLP 2016-10-30 21:42:42 2018-09-10 21:06:27 5921 Valencia Cir ## 3 AVwc252WIN2L1WUfpqLP 2016-10-30 21:42:42 2018-09-10 21:06:27 5921 Valencia Cir ## 4 AVwdOclqIN2L1WUfti38 2015-11-28 19:19:35 2018-09-10 21:06:16 7520 Teague Rd ## 5 AVwdOclqIN2L1WUfti38 2015-11-28 19:19:35 2018-09-10 21:06:16 7520 Teague Rd ## 6 AVwdOclqIN2L1WUfti38 2015-11-28 19:19:35 2018-09-10 21:06:16 7520 Teague Rd ... df1 <- df %>% mutate(dateAdded = str_replace(string = dateAdded, pattern = "-",replacement = ".")) head(df1)
🌐
Spark By {Examples}
sparkbyexamples.com › home › r programming › r – str_replace() to replace matched patterns in a string.
R - str_replace() to Replace Matched Patterns in a String. - Spark By {Examples}
March 27, 2024 - R str_replace() and str_replace_all() are used to replace values of a string column based on matched patterns ( pattern matching with regex - regular
🌐
RDocumentation
rdocumentation.org › packages › stringr › versions › 0.5 › topics › str_replace_all
str_replace_all function - RDocumentation
Learn R Programming · stringr (version 0.5) Replace all occurrences of a matched pattern in a string. str_replace_all(string, pattern, replacement) string · input character vector · pattern · pattern to look for, as defined by a POSIX regular expression. See the ``Extended Regular Expressions'' ...
🌐
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Built-in Modules Random Module ... Python Interview Q&A Python Training ... The replace() method replaces a specified phrase with another specified phrase....
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
It is exposed as a separate function ... of arguments, rather than unpacking and repacking the dictionary as individual arguments using the *args and **kwargs syntax. vformat() does the work of breaking up the format string into character data and replacement fields....