EVOLUTION-MANAGER
Edit File: language.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: Selection language</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 language {tidyselect}"><tr><td>language {tidyselect}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Selection language</h2> <h3>Description</h3> <h4>Overview of selection features:</h4> <p>Tidyverse selections implement a dialect of R where operators make it easy to select variables: </p> <ul> <li> <p><code>:</code> for selecting a range of consecutive variables. </p> </li> <li> <p><code>!</code> for taking the complement of a set of variables. </p> </li> <li> <p><code>&</code> and <code>|</code> for selecting the intersection or the union of two sets of variables. </p> </li> <li> <p><code>c()</code> for combining selections. </p> </li></ul> <p>In addition, you can use <strong>selection helpers</strong>. Some helpers select specific columns: </p> <ul> <li> <p><code><a href="everything.html">everything()</a></code>: Matches all variables. </p> </li> <li> <p><code><a href="last_col.html">last_col()</a></code>: Select last variable, possibly with an offset. </p> </li></ul> <p>These helpers select variables by matching patterns in their names: </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="ends_with.html">ends_with()</a></code>: Ends with a suffix. </p> </li> <li> <p><code><a href="contains.html">contains()</a></code>: Contains a literal string. </p> </li> <li> <p><code><a href="matches.html">matches()</a></code>: Matches a regular expression. </p> </li> <li> <p><code><a href="num_range.html">num_range()</a></code>: Matches a numerical range like x01, x02, x03. </p> </li></ul> <p>These helpers select variables from a character vector: </p> <ul> <li> <p><code><a href="all_of.html">all_of()</a></code>: Matches variable names in a character vector. All names must be present, otherwise an out-of-bounds error is thrown. </p> </li> <li> <p><code><a href="any_of.html">any_of()</a></code>: Same as <code>all_of()</code>, except that no error is thrown for names that don't exist. </p> </li></ul> <p>This helper selects variables with a function: </p> <ul> <li> <p><code><a href="where.html">where()</a></code>: Applies a function to all variables and selects those for which the function returns <code>TRUE</code>. </p> </li></ul> <h3>Simple examples</h3> <p>Here we show the usage for the basic selection operators. See the specific help pages to learn about helpers like <code><a href="starts_with.html">starts_with()</a></code>. </p> <p>The selection language 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>Select variables by name:<div class="r"></p> <pre>starwars %>% select(height) #> # A tibble: 87 x 1 #> height #> <int> #> 1 172 #> 2 167 #> 3 96 #> 4 202 #> # ... with 83 more rows iris %>% pivot_longer(Sepal.Length) #> # A tibble: 150 x 6 #> Sepal.Width Petal.Length Petal.Width Species name value #> <dbl> <dbl> <dbl> <fct> <chr> <dbl> #> 1 3.5 1.4 0.2 setosa Sepal.Length 5.1 #> 2 3 1.4 0.2 setosa Sepal.Length 4.9 #> 3 3.2 1.3 0.2 setosa Sepal.Length 4.7 #> 4 3.1 1.5 0.2 setosa Sepal.Length 4.6 #> # ... with 146 more rows </pre></div> <p>Select multiple variables by separating them with commas. Note how the order of columns is determined by the order of inputs:<div class="r"></p> <pre>starwars %>% select(homeworld, height, mass) #> # A tibble: 87 x 3 #> homeworld height mass #> <chr> <int> <dbl> #> 1 Tatooine 172 77 #> 2 Tatooine 167 75 #> 3 Naboo 96 32 #> 4 Tatooine 202 136 #> # ... with 83 more rows </pre></div> <p>Functions like <code>tidyr::pivot_longer()</code> don't take variables with dots. In this case use <code>c()</code> to select multiple variables:<div class="r"></p> <pre>iris %>% pivot_longer(c(Sepal.Length, Petal.Length)) #> # A tibble: 300 x 5 #> Sepal.Width Petal.Width Species name value #> <dbl> <dbl> <fct> <chr> <dbl> #> 1 3.5 0.2 setosa Sepal.Length 5.1 #> 2 3.5 0.2 setosa Petal.Length 1.4 #> 3 3 0.2 setosa Sepal.Length 4.9 #> 4 3 0.2 setosa Petal.Length 1.4 #> # ... with 296 more rows </pre></div> <h4>Operators:</h4> <p>The <code>:</code> operator selects a range of consecutive variables:<div class="r"></p> <pre>starwars %>% select(name:mass) #> # A tibble: 87 x 3 #> name height mass #> <chr> <int> <dbl> #> 1 Luke Skywalker 172 77 #> 2 C-3PO 167 75 #> 3 R2-D2 96 32 #> 4 Darth Vader 202 136 #> # ... with 83 more rows </pre></div> <p>The <code>!</code> operator negates a selection:<div class="r"></p> <pre>starwars %>% select(!(name:mass)) #> # A tibble: 87 x 11 #> hair_color skin_color eye_color birth_year sex gender homeworld species #> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> #> 1 blond fair blue 19 male mascu~ Tatooine Human #> 2 <NA> gold yellow 112 none mascu~ Tatooine Droid #> 3 <NA> white, bl~ red 33 none mascu~ Naboo Droid #> 4 none white yellow 41.9 male mascu~ Tatooine Human #> # ... with 83 more rows, and 3 more variables: films <list>, vehicles <list>, #> # starships <list> iris %>% select(!c(Sepal.Length, Petal.Length)) #> # A tibble: 150 x 3 #> Sepal.Width Petal.Width Species #> <dbl> <dbl> <fct> #> 1 3.5 0.2 setosa #> 2 3 0.2 setosa #> 3 3.2 0.2 setosa #> 4 3.1 0.2 setosa #> # ... with 146 more rows iris %>% select(!ends_with("Width")) #> # A tibble: 150 x 3 #> Sepal.Length Petal.Length Species #> <dbl> <dbl> <fct> #> 1 5.1 1.4 setosa #> 2 4.9 1.4 setosa #> 3 4.7 1.3 setosa #> 4 4.6 1.5 setosa #> # ... with 146 more rows </pre></div> <p><code>&</code> and <code>|</code> take the intersection or the union of two selections:<div class="r"></p> <pre>iris %>% select(starts_with("Petal") & ends_with("Width")) #> # A tibble: 150 x 1 #> Petal.Width #> <dbl> #> 1 0.2 #> 2 0.2 #> 3 0.2 #> 4 0.2 #> # ... with 146 more rows iris %>% select(starts_with("Petal") | ends_with("Width")) #> # A tibble: 150 x 3 #> Petal.Length Petal.Width Sepal.Width #> <dbl> <dbl> <dbl> #> 1 1.4 0.2 3.5 #> 2 1.4 0.2 3 #> 3 1.3 0.2 3.2 #> 4 1.5 0.2 3.1 #> # ... with 146 more rows </pre></div> <p>To take the difference between two selections, combine the <code>&</code> and <code>!</code> operators:<div class="r"></p> <pre>iris %>% select(starts_with("Petal") & !ends_with("Width")) #> # A tibble: 150 x 1 #> Petal.Length #> <dbl> #> 1 1.4 #> 2 1.4 #> 3 1.3 #> 4 1.5 #> # ... with 146 more rows </pre></div> <h3>Details</h3> <p>The order of selected columns is determined by the inputs. </p> <ul> <li> <p><code>all_of(c("foo", "bar"))</code> selects <code>"foo"</code> first. </p> </li> <li> <p><code>c(starts_with("c"), starts_with("d"))</code> selects all columns starting with <code>"c"</code> first, then all columns starting with <code>"d"</code>. </p> </li></ul> <hr /><div style="text-align: center;">[Package <em>tidyselect</em> version 1.1.0 <a href="00Index.html">Index</a>]</div> </body></html>