EVOLUTION-MANAGER
Edit File: str_replace.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Replace matched patterns in a string.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for str_replace {stringr}"><tr><td>str_replace {stringr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Replace matched patterns in a string.</h2> <h3>Description</h3> <p>Vectorised over <code>string</code>, <code>pattern</code> and <code>replacement</code>. </p> <h3>Usage</h3> <pre> str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>string</code></td> <td> <p>Input vector. Either a character vector, or something coercible to one.</p> </td></tr> <tr valign="top"><td><code>pattern</code></td> <td> <p>Pattern to look for. </p> <p>The default interpretation is a regular expression, as described in <a href="../../stringi/html/stringi-search-regex.html">stringi::stringi-search-regex</a>. Control options with <code><a href="modifiers.html">regex()</a></code>. </p> <p>Match a fixed string (i.e. by comparing only bytes), using <code><a href="modifiers.html">fixed()</a></code>. This is fast, but approximate. Generally, for matching human text, you'll want <code><a href="modifiers.html">coll()</a></code> which respects character matching rules for the specified locale.</p> </td></tr> <tr valign="top"><td><code>replacement</code></td> <td> <p>A character vector of replacements. Should be either length one, or the same length as <code>string</code> or <code>pattern</code>. References of the form <code>\1</code>, <code>\2</code>, etc will be replaced with the contents of the respective matched group (created by <code>()</code>). </p> <p>To perform multiple replacements in each element of <code>string</code>, pass a named vector (<code>c(pattern1 = replacement1)</code>) to <code>str_replace_all</code>. Alternatively, pass a function to <code>replacement</code>: it will be called once for each match and its return value will be used to replace the match. </p> <p>To replace the complete string with <code>NA</code>, use <code>replacement = NA_character_</code>.</p> </td></tr> </table> <h3>Value</h3> <p>A character vector. </p> <h3>See Also</h3> <p><code><a href="str_replace_na.html">str_replace_na()</a></code> to turn missing values into "NA"; <code><a href="../../stringi/html/stri_replace.html">stri_replace()</a></code> for the underlying implementation. </p> <h3>Examples</h3> <pre> fruits <- c("one apple", "two pears", "three bananas") str_replace(fruits, "[aeiou]", "-") str_replace_all(fruits, "[aeiou]", "-") str_replace_all(fruits, "[aeiou]", toupper) str_replace_all(fruits, "b", NA_character_) str_replace(fruits, "([aeiou])", "") str_replace(fruits, "([aeiou])", "\\1\\1") str_replace(fruits, "[aeiou]", c("1", "2", "3")) str_replace(fruits, c("a", "e", "i"), "-") # If you want to apply multiple patterns and replacements to the same # string, pass a named vector to pattern. fruits %>% str_c(collapse = "---") %>% str_replace_all(c("one" = "1", "two" = "2", "three" = "3")) # Use a function for more sophisticated replacement. 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", ], max = 255) } x <- c( "Roses are red, violets are blue", "My favourite colour is green" ) str_replace_all(x, colours, col2hex) </pre> <hr /><div style="text-align: center;">[Package <em>stringr</em> version 1.4.0 <a href="00Index.html">Index</a>]</div> </body></html>