EVOLUTION-MANAGER
Edit File: starts_with.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: Select variables that match a pattern</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 starts_with {tidyselect}"><tr><td>starts_with {tidyselect}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Select variables that match a pattern</h2> <h3>Description</h3> <p>These <a href="language.html">selection helpers</a> match variables according to a given pattern. </p> <ul> <li> <p><code><a href="starts_with.html">starts_with()</a></code>: Starts with a prefix. </p> </li> <li> <p><code><a href="starts_with.html">ends_with()</a></code>: Ends with a suffix. </p> </li> <li> <p><code><a href="starts_with.html">contains()</a></code>: Contains a literal string. </p> </li> <li> <p><code><a href="starts_with.html">matches()</a></code>: Matches a regular expression. </p> </li> <li> <p><code><a href="starts_with.html">num_range()</a></code>: Matches a numerical range like x01, x02, x03. </p> </li></ul> <h3>Usage</h3> <pre> starts_with(match, ignore.case = TRUE, vars = NULL) ends_with(match, ignore.case = TRUE, vars = NULL) contains(match, ignore.case = TRUE, vars = NULL) matches(match, ignore.case = TRUE, perl = FALSE, vars = NULL) num_range(prefix, range, width = NULL, vars = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>match</code></td> <td> <p>A character vector. If length > 1, the union of the matches is taken.</p> </td></tr> <tr valign="top"><td><code>ignore.case</code></td> <td> <p>If <code>TRUE</code>, the default, ignores case when matching names.</p> </td></tr> <tr valign="top"><td><code>vars</code></td> <td> <p>A character vector of variable names. If not supplied, the variables are taken from the current selection context (as established by functions like <code>select()</code> or <code>pivot_longer()</code>).</p> </td></tr> <tr valign="top"><td><code>perl</code></td> <td> <p>Should Perl-compatible regexps be used?</p> </td></tr> <tr valign="top"><td><code>prefix</code></td> <td> <p>A prefix that starts the numeric range.</p> </td></tr> <tr valign="top"><td><code>range</code></td> <td> <p>A sequence of integers, like <code>1:5</code>.</p> </td></tr> <tr valign="top"><td><code>width</code></td> <td> <p>Optionally, the "width" of the numeric range. For example, a range of 2 gives "01", a range of three "001", etc.</p> </td></tr> </table> <h3>Examples</h3> <p>Selection helpers can be used in functions like <code>dplyr::select()</code> or <code>tidyr::pivot_longer()</code>. Let's first attach the tidyverse:<div class="r"></p> <pre>library(tidyverse) # For better printing iris <- as_tibble(iris) </pre></div> <p><code>starts_with()</code> selects all variables matching a prefix and <code>ends_with()</code> matches a suffix:<div class="r"></p> <pre>iris %>% select(starts_with("Sepal")) #> # A tibble: 150 x 2 #> Sepal.Length Sepal.Width #> <dbl> <dbl> #> 1 5.1 3.5 #> 2 4.9 3 #> 3 4.7 3.2 #> 4 4.6 3.1 #> # ... with 146 more rows iris %>% select(ends_with("Width")) #> # A tibble: 150 x 2 #> Sepal.Width Petal.Width #> <dbl> <dbl> #> 1 3.5 0.2 #> 2 3 0.2 #> 3 3.2 0.2 #> 4 3.1 0.2 #> # ... with 146 more rows </pre></div> <p>You can supply multiple prefixes or suffixes. Note how the order of variables depends on the order of the suffixes and prefixes:<div class="r"></p> <pre>iris %>% select(starts_with(c("Petal", "Sepal"))) #> # A tibble: 150 x 4 #> Petal.Length Petal.Width Sepal.Length Sepal.Width #> <dbl> <dbl> <dbl> <dbl> #> 1 1.4 0.2 5.1 3.5 #> 2 1.4 0.2 4.9 3 #> 3 1.3 0.2 4.7 3.2 #> 4 1.5 0.2 4.6 3.1 #> # ... with 146 more rows iris %>% select(ends_with(c("Width", "Length"))) #> # A tibble: 150 x 4 #> Sepal.Width Petal.Width Sepal.Length Petal.Length #> <dbl> <dbl> <dbl> <dbl> #> 1 3.5 0.2 5.1 1.4 #> 2 3 0.2 4.9 1.4 #> 3 3.2 0.2 4.7 1.3 #> 4 3.1 0.2 4.6 1.5 #> # ... with 146 more rows </pre></div> <p><code>contains()</code> selects columns whose names contain a word:<div class="r"></p> <pre>iris %>% select(contains("al")) #> # A tibble: 150 x 4 #> Sepal.Length Sepal.Width Petal.Length Petal.Width #> <dbl> <dbl> <dbl> <dbl> #> 1 5.1 3.5 1.4 0.2 #> 2 4.9 3 1.4 0.2 #> 3 4.7 3.2 1.3 0.2 #> 4 4.6 3.1 1.5 0.2 #> # ... with 146 more rows </pre></div> <p>These helpers do not use regular expressions. To select with a regexp use <code>matches()</code><div class="r"></p> <pre># [pt] is matched literally: iris %>% select(contains("[pt]al")) #> # A tibble: 150 x 0 # [pt] is interpreted as a regular expression iris %>% select(matches("[pt]al")) #> # A tibble: 150 x 4 #> Sepal.Length Sepal.Width Petal.Length Petal.Width #> <dbl> <dbl> <dbl> <dbl> #> 1 5.1 3.5 1.4 0.2 #> 2 4.9 3 1.4 0.2 #> 3 4.7 3.2 1.3 0.2 #> 4 4.6 3.1 1.5 0.2 #> # ... with 146 more rows </pre></div> <p><code>starts_with()</code> selects all variables starting with a prefix. To select a range, use <code>num_range()</code>. Compare:<div class="r"></p> <pre>billboard %>% select(starts_with("wk")) #> # A tibble: 317 x 76 #> wk1 wk2 wk3 wk4 wk5 wk6 wk7 wk8 wk9 wk10 wk11 wk12 wk13 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 87 82 72 77 87 94 99 NA NA NA NA NA NA #> 2 91 87 92 NA NA NA NA NA NA NA NA NA NA #> 3 81 70 68 67 66 57 54 53 51 51 51 51 47 #> 4 76 76 72 69 67 65 55 59 62 61 61 59 61 #> # ... with 313 more rows, and 63 more variables: wk14 <dbl>, wk15 <dbl>, #> # wk16 <dbl>, wk17 <dbl>, wk18 <dbl>, wk19 <dbl>, wk20 <dbl>, wk21 <dbl>, ... billboard %>% select(num_range("wk", 10:15)) #> # A tibble: 317 x 6 #> wk10 wk11 wk12 wk13 wk14 wk15 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 NA NA NA NA NA NA #> 2 NA NA NA NA NA NA #> 3 51 51 51 47 44 38 #> 4 61 61 59 61 66 72 #> # ... with 313 more rows </pre></div> <h3>See Also</h3> <p>The <a href="language.html">selection language</a> page, which includes links to other selection helpers. </p> <hr /><div style="text-align: center;">[Package <em>tidyselect</em> version 1.1.0 <a href="00Index.html">Index</a>]</div> </body></html>