| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/helpers-vector.R |
| 3 |
\name{all_of} |
| 4 |
\alias{all_of} |
| 5 |
\alias{any_of} |
| 6 |
\title{Select variables from character vectors} |
| 7 |
\usage{ |
| 8 |
all_of(x) |
| 9 |
|
| 10 |
any_of(x, ..., vars = NULL) |
| 11 |
} |
| 12 |
\arguments{ |
| 13 |
\item{x}{A vector of character names or numeric locations.} |
| 14 |
|
| 15 |
\item{...}{These dots are for future extensions and must be empty.} |
| 16 |
|
| 17 |
\item{vars}{A character vector of variable names. If not supplied, |
| 18 |
the variables are taken from the current selection context (as |
| 19 |
established by functions like \code{select()} or \code{pivot_longer()}).} |
| 20 |
} |
| 21 |
\description{ |
| 22 |
These \link[=language]{selection helpers} select variables |
| 23 |
contained in a character vector. They are especially useful for |
| 24 |
programming with selecting functions. |
| 25 |
\itemize{ |
| 26 |
\item \code{\link[=all_of]{all_of()}} is for strict selection. If any of the variables in |
| 27 |
the character vector is missing, an error is thrown. |
| 28 |
\item \code{\link[=any_of]{any_of()}} doesn't check for missing variables. It is especially |
| 29 |
useful with negative selections, when you would like to make sure |
| 30 |
a variable is removed. |
| 31 |
} |
| 32 |
|
| 33 |
The order of selected columns is determined by the order in the |
| 34 |
vector. |
| 35 |
} |
| 36 |
\section{Examples}{ |
| 37 |
|
| 38 |
|
| 39 |
Selection helpers can be used in functions like \code{dplyr::select()} |
| 40 |
or \code{tidyr::pivot_longer()}. Let's first attach the tidyverse: |
| 41 |
|
| 42 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{library(tidyverse) |
| 43 |
|
| 44 |
# For better printing |
| 45 |
iris <- as_tibble(iris) |
| 46 |
}\if{html}{\out{</div>}} |
| 47 |
|
| 48 |
It is a common to have a names of variables in a vector. |
| 49 |
|
| 50 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{vars <- c("Sepal.Length", "Sepal.Width") |
| 51 |
|
| 52 |
iris[, vars] |
| 53 |
#> # A tibble: 150 x 2 |
| 54 |
#> Sepal.Length Sepal.Width |
| 55 |
#> <dbl> <dbl> |
| 56 |
#> 1 5.1 3.5 |
| 57 |
#> 2 4.9 3 |
| 58 |
#> 3 4.7 3.2 |
| 59 |
#> 4 4.6 3.1 |
| 60 |
#> # i 146 more rows |
| 61 |
}\if{html}{\out{</div>}} |
| 62 |
|
| 63 |
To refer to these variables in selecting function, use \code{all_of()}: |
| 64 |
|
| 65 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(all_of(vars)) |
| 66 |
#> # A tibble: 150 x 2 |
| 67 |
#> Sepal.Length Sepal.Width |
| 68 |
#> <dbl> <dbl> |
| 69 |
#> 1 5.1 3.5 |
| 70 |
#> 2 4.9 3 |
| 71 |
#> 3 4.7 3.2 |
| 72 |
#> 4 4.6 3.1 |
| 73 |
#> # i 146 more rows |
| 74 |
|
| 75 |
iris \%>\% pivot_longer(all_of(vars)) |
| 76 |
#> # A tibble: 300 x 5 |
| 77 |
#> Petal.Length Petal.Width Species name value |
| 78 |
#> <dbl> <dbl> <fct> <chr> <dbl> |
| 79 |
#> 1 1.4 0.2 setosa Sepal.Length 5.1 |
| 80 |
#> 2 1.4 0.2 setosa Sepal.Width 3.5 |
| 81 |
#> 3 1.4 0.2 setosa Sepal.Length 4.9 |
| 82 |
#> 4 1.4 0.2 setosa Sepal.Width 3 |
| 83 |
#> # i 296 more rows |
| 84 |
}\if{html}{\out{</div>}} |
| 85 |
|
| 86 |
If any of the variable is missing from the data frame, that's an error: |
| 87 |
|
| 88 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(all_of(vars)) |
| 89 |
#> Error: |
| 90 |
#> i In argument: `all_of(vars)`. |
| 91 |
#> Caused by error in `all_of()` at rlang/R/eval-tidy.R:121:3: |
| 92 |
#> ! Can't subset elements that don't exist. |
| 93 |
#> x Elements `Sepal.Length` and `Sepal.Width` don't exist. |
| 94 |
}\if{html}{\out{</div>}} |
| 95 |
|
| 96 |
Use \code{any_of()} to allow missing variables: |
| 97 |
|
| 98 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(any_of(vars)) |
| 99 |
#> # A tibble: 87 x 0 |
| 100 |
}\if{html}{\out{</div>}} |
| 101 |
|
| 102 |
\code{any_of()} is especially useful to remove variables from a data |
| 103 |
frame because calling it again does not cause an error: |
| 104 |
|
| 105 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(-any_of(vars)) |
| 106 |
#> # A tibble: 150 x 3 |
| 107 |
#> Petal.Length Petal.Width Species |
| 108 |
#> <dbl> <dbl> <fct> |
| 109 |
#> 1 1.4 0.2 setosa |
| 110 |
#> 2 1.4 0.2 setosa |
| 111 |
#> 3 1.3 0.2 setosa |
| 112 |
#> 4 1.5 0.2 setosa |
| 113 |
#> # i 146 more rows |
| 114 |
|
| 115 |
iris \%>\% select(-any_of(vars)) \%>\% select(-any_of(vars)) |
| 116 |
#> # A tibble: 150 x 3 |
| 117 |
#> Petal.Length Petal.Width Species |
| 118 |
#> <dbl> <dbl> <fct> |
| 119 |
#> 1 1.4 0.2 setosa |
| 120 |
#> 2 1.4 0.2 setosa |
| 121 |
#> 3 1.3 0.2 setosa |
| 122 |
#> 4 1.5 0.2 setosa |
| 123 |
#> # i 146 more rows |
| 124 |
}\if{html}{\out{</div>}} |
| 125 |
} |
| 126 |
|
| 127 |
\seealso{ |
| 128 |
The \link[=language]{selection language} page, which includes links to other selection helpers. |
| 129 |
} |