To access function from an library you have either load and attach it:

library(stringr) # Or require(stringr)
str_replace(x, "XXXX", "XXxxx")

or use double colon operator:

stringr::str_replace(x, "XXXX", "XXxxx")

Unfortunately :: is quite expensive so if you prefer to keep your namespace clean you should consider creating a local binding:

str_replace <- stringr::str_replace
str_replace(x, "XXXX", "XXxxx")

On a side not using internal API with ::: is probably not the best idea. Ignoring good practices it is simply far to slow to be useful in practice.

Answer from zero323 on Stack Overflow
Discussions

Use of string_replace()
My data looked like this head(ttrigram) word1 word2 frequency 1 enjoying_case presentations 77 2 case_presentations students 77 3 presentations_students w 77 4 students_w good 77 5 w_good luck 77 6 good_luck students 77 I was trying to replace the undersco... More on forum.posit.co
🌐 forum.posit.co
1
0
May 29, 2019
Trouble with str_replace_all
I would like to do multiple replacements to the same string. The following example is given in the documentation of package stringr: x More on github.com
🌐 github.com
6
January 6, 2017
[R] arrow R package: support for stringr::str_replace_all() incomplete
Describe the bug, including details regarding any error messages, version, and platform. The arrow R package claims to support the stringr::str_replace_all() function, however, it does not support ... More on github.com
🌐 github.com
3
December 12, 2023
Can not run the code of str_remove
Below codes I copied from an example in this community: fruits % paste0("(", ., : could not find function "str_replace_all" and str_remove_all(fruits, "[aeiou]") such: Error during wrapup: could not find function "str_remove_all" Can anyone ple... More on forum.posit.co
🌐 forum.posit.co
1
0
May 4, 2022
Top answer
1 of 1
11

Edit: It appears @akrun has deleted his answer, so it is reproduced below. I made the (perhaps faulty) assumption you wished to transform the whole dataframe, and that the dataframe was appropriately formatted. These issues are avoided with clearer questions, and sample data.

It's a little hard to tell with no clue as to the values of your global variables and no data (you can generate fake data, btw, as long as it presents the same issue), but my guess is below.

When piping, the previous result is piped in as the first argument of the next function. You can see this in the error message: Error in str_replace_all(., string, pattern, replacement) -- the ., shows the piped in argument. Here, the first argument is "string". Therefore, the piped in result is being used as string, "string" is used as pattern, "pattern" is being used for replacement, and the "" you put in for replacement is left as an unused argument, causing your error.

Might help to use str_replace_all(pattern, replacement), or specify arguments: str_replace_all(pattern = pattern, replacement = replacement)

Ex.

data <- as.data.frame(matrix(ncol=2, nrow=2))
data$V1 <- c("  NA", "foo")
data$V2 <- c("bar", "boo")
data %>%
    str_replace_all("oo", "xx")

If you only wish to transform one column (from @akrun): Simply use mutate to create a new column based off of the preexisting column. If you wish to replace the column, give it the same name:

Ex.

data <- as.data.frame(matrix(ncol=2, nrow=2))
data$V1 <- c("  NA", "foo")
data$V2 <- c("bar", "boo")
data
    V1  V2
1   NA bar
2  foo boo

#new column
data %>%
    mutate(new = str_replace_all(V1, "oo", "xx"))

    V1  V2  new
1   NA bar   NA
2  foo boo  fxx

#column replacement
data %>%
    mutate(V1 = str_replace_all(V1, "oo", "xx"))

    V1  V2
1   NA bar
2  fxx boo
🌐
PHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
A funny thing to note is that: <?php $input = "SELSELECTECT"; echo str_replace("SELECT", null, $input); // = SELECT ?> Yeah you could loop it, but str_replace was never meant to be used this way. There are proper ways to protect against SQL Injections, such as using prepared statements (placeholders). ... <?php /** * Convert foreign 8859-1 characters into HTML entities. * * @param string $str * The string being parsed. * * @return string * The converted string. */ public static function convert_chars_to_entities( $str ) { $str = str_replace( 'À', '&#192;', $str ); $str = str_replace( 'Á', '&
🌐
GitHub
github.com › tidyverse › stringr › issues › 144
Trouble with str_replace_all · Issue #144 · tidyverse/stringr
January 6, 2017 - The following example is given in the documentation of package stringr: x <- c("1 house", "2 cars", "3 people") str_replace_all(x, c("1" = "one", "2" = "two", "3" = "three"))
Author: tidyverse
🌐
Tidyverse
stringr.tidyverse.org › news › index.html
Changelog - stringr - Tidyverse
November 4, 2025 - All relevant stringr functions now preserve names (@jonovik, #575). str_like(ignore_case) is deprecated, with str_like() now always case sensitive to better follow the conventions of the SQL LIKE operator (@edward-burn, #543). In str_replace_all(), a replacement function now receives all values ...
Find elsewhere
🌐
GitHub
github.com › apache › arrow › issues › 39191
[R] arrow R package: support for stringr::str_replace_all() incomplete · Issue #39191 · apache/arrow
December 12, 2023 - Describe the bug, including details regarding any error messages, version, and platform. The arrow R package claims to support the stringr::str_replace_all() function, however, it does not support using a vector of pattern/replacements a...
Author: apache
🌐
Stack Overflow
stackoverflow.com › questions › 39144743 › using-a-function-in-str-replace
Using a function in str_replace
August 25, 2016 - I think the problem is the return result of str_replace it cant be concatenated using "." apparently... ... The php function does something completely different to what the js function does.
🌐
GitHub
github.com › AndreaCirilloAC › updateR › issues
Issues · AndreaCirilloAC/updateR
update your R version in a breeze ( on OSX) √. Contribute to AndreaCirilloAC/updateR development by creating an account on GitHub.
Author: AndreaCirilloAC
🌐
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.
🌐
CRAN
cran.r-project.org › web › packages › stringr › stringr.pdf pdf
Package ‘stringr’ November 4, 2025
You can not match boundaries, including "", with this function. ... A character vector the same length as string/pattern. ... Input vector. Either a character vector, or something coercible to one. ... Pattern to look for. The default interpretation is a regular expression, as described in stringi::about_search_regex. Control options with regex(). For str_replace...
🌐
Rdrr.io
rdrr.io › github › tidyverse › stringr › man › str_replace.html
str_replace: Replace matches with new text in tidyverse/stringr: Simple, Consistent Wrappers for Common String Operations
August 23, 2024 - This example # replaces colour names with their hex values. colours <- str_c("\\b", colors(), "\\b", collapse="|") col2hex <- function(col) { rgb <- col2rgb(col) rgb(rgb["red", ], rgb["green", ], rgb["blue", ], maxColorValue = 255) } x <- c( "Roses are red, violets are blue", "My favourite colour is green" ) str_replace_all(x, colours, col2hex) tidyverse/stringr documentation built on Aug. 23, 2024, 7:03 a.m. ... Note that we can't provide technical support on individual packages.
🌐
CRAN
cran.r-project.org › web › packages › stringr › readme › README.html
readme - CRAN - R Project
If you’re not familiar with strings, ... set covering almost anything you can imagine. If you find that stringr is missing a function that you need, try looking in stringi....