EVOLUTION-MANAGER
Edit File: all_of.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 from character vectors</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 all_of {tidyselect}"><tr><td>all_of {tidyselect}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Select variables from character vectors</h2> <h3>Description</h3> <p>These <a href="language.html">selection helpers</a> select variables contained in a character vector. They are especially useful for programming with selecting functions. </p> <ul> <li> <p><code><a href="all_of.html">all_of()</a></code> is for strict selection. If any of the variables in the character vector is missing, an error is thrown. </p> </li> <li> <p><code><a href="all_of.html">any_of()</a></code> doesn't check for missing variables. It is especially useful with negative selections, when you would like to make sure a variable is removed. </p> </li></ul> <p>The order of selected columns is determined by the order in the vector. </p> <h3>Usage</h3> <pre> all_of(x) any_of(x, ..., vars = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>A vector of character names or numeric locations.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>These dots are for future extensions and must be empty.</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> </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>It is a common to have a names of variables in a vector.<div class="r"></p> <pre>vars <- c("Sepal.Length", "Sepal.Width") iris[, vars] #> # 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 </pre></div> <p>To refer to these variables in selecting function, use <code>all_of()</code>:<div class="r"></p> <pre>iris %>% select(all_of(vars)) #> # 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 %>% pivot_longer(all_of(vars)) #> # A tibble: 300 x 5 #> Petal.Length Petal.Width Species name value #> <dbl> <dbl> <fct> <chr> <dbl> #> 1 1.4 0.2 setosa Sepal.Length 5.1 #> 2 1.4 0.2 setosa Sepal.Width 3.5 #> 3 1.4 0.2 setosa Sepal.Length 4.9 #> 4 1.4 0.2 setosa Sepal.Width 3 #> # ... with 296 more rows </pre></div> <p>If any of the variable is missing from the data frame, that's an error:<div class="r"></p> <pre>starwars %>% select(all_of(vars)) </pre></div><pre>## Error: Can't subset columns that don't exist. ## x Columns `Sepal.Length` and `Sepal.Width` don't exist. </pre> <p>Use <code>any_of()</code> to allow missing variables:<div class="r"></p> <pre>starwars %>% select(any_of(vars)) #> # A tibble: 87 x 0 </pre></div> <p><code>any_of()</code> is especially useful to remove variables from a data frame because calling it again does not cause an error:<div class="r"></p> <pre>iris %>% select(-any_of(vars)) #> # A tibble: 150 x 3 #> Petal.Length Petal.Width Species #> <dbl> <dbl> <fct> #> 1 1.4 0.2 setosa #> 2 1.4 0.2 setosa #> 3 1.3 0.2 setosa #> 4 1.5 0.2 setosa #> # ... with 146 more rows iris %>% select(-any_of(vars)) %>% select(-any_of(vars)) #> # A tibble: 150 x 3 #> Petal.Length Petal.Width Species #> <dbl> <dbl> <fct> #> 1 1.4 0.2 setosa #> 2 1.4 0.2 setosa #> 3 1.3 0.2 setosa #> 4 1.5 0.2 setosa #> # ... with 146 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>