EVOLUTION-MANAGER
Edit File: str_sub.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: Extract and replace substrings from a character vector.</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_sub {stringr}"><tr><td>str_sub {stringr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Extract and replace substrings from a character vector.</h2> <h3>Description</h3> <p><code>str_sub</code> will recycle all arguments to be the same length as the longest argument. If any arguments are of length 0, the output will be a zero length character vector. </p> <h3>Usage</h3> <pre> str_sub(string, start = 1L, end = -1L) str_sub(string, start = 1L, end = -1L, omit_na = FALSE) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>string</code></td> <td> <p>input character vector.</p> </td></tr> <tr valign="top"><td><code>start, end</code></td> <td> <p>Two integer vectors. <code>start</code> gives the position of the first character (defaults to first), <code>end</code> gives the position of the last (defaults to last character). Alternatively, pass a two-column matrix to <code>start</code>. </p> <p>Negative values count backwards from the last character.</p> </td></tr> <tr valign="top"><td><code>omit_na</code></td> <td> <p>Single logical value. If <code>TRUE</code>, missing values in any of the arguments provided will result in an unchanged input.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>replacement string</p> </td></tr> </table> <h3>Details</h3> <p>Substrings are inclusive - they include the characters at both start and end positions. <code>str_sub(string, 1, -1)</code> will return the complete substring, from the first character to the last. </p> <h3>Value</h3> <p>A character vector of substring from <code>start</code> to <code>end</code> (inclusive). Will be length of longest input argument. </p> <h3>See Also</h3> <p>The underlying implementation in <code><a href="../../stringi/html/stri_sub.html">stringi::stri_sub()</a></code> </p> <h3>Examples</h3> <pre> hw <- "Hadley Wickham" str_sub(hw, 1, 6) str_sub(hw, end = 6) str_sub(hw, 8, 14) str_sub(hw, 8) str_sub(hw, c(1, 8), c(6, 14)) # Negative indices str_sub(hw, -1) str_sub(hw, -7) str_sub(hw, end = -7) # Alternatively, you can pass in a two colum matrix, as in the # output from str_locate_all pos <- str_locate_all(hw, "[aeio]")[[1]] str_sub(hw, pos) str_sub(hw, pos[, 1], pos[, 2]) # Vectorisation str_sub(hw, seq_len(str_length(hw))) str_sub(hw, end = seq_len(str_length(hw))) # Replacement form x <- "BBCDEF" str_sub(x, 1, 1) <- "A"; x str_sub(x, -1, -1) <- "K"; x str_sub(x, -2, -2) <- "GHIJ"; x str_sub(x, 2, -2) <- ""; x # If you want to keep the original if some argument is NA, # use omit_na = TRUE x1 <- x2 <- x3 <- x4 <- "AAA" str_sub(x1, 1, NA) <- "B" str_sub(x2, 1, 2) <- NA str_sub(x3, 1, NA, omit_na = TRUE) <- "B" str_sub(x4, 1, 2, omit_na = TRUE) <- NA x1; x2; x3; x4 </pre> <hr /><div style="text-align: center;">[Package <em>stringr</em> version 1.4.0 <a href="00Index.html">Index</a>]</div> </body></html>