These selection helpers select variables contained in a character vector. They are especially useful for programming with selecting functions.
all_of()
is for strict selection. If any of the variables in the character vector is missing, an error is thrown.any_of()
doesn't check for missing variables. It is especially useful with negative selections, when you would like to make sure a variable is removed.
The order of selected columns is determined by the order in the vector.
Arguments
- x
A vector of character names or numeric locations.
- ...
These dots are for future extensions and must be empty.
- vars
A character vector of variable names. If not supplied, the variables are taken from the current selection context (as established by functions like
select()
orpivot_longer()
).
Examples
Selection helpers can be used in functions like dplyr::select()
or tidyr::pivot_longer()
. Let's first attach the tidyverse:
It is a common to have a names of variables in a vector.
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
#> # i 146 more rows
To refer to these variables in selecting function, use all_of()
:
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
#> # i 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
#> # i 296 more rows
If any of the variable is missing from the data frame, that's an error:
starwars %>% select(all_of(vars))
#> Error:
#> i In argument: `all_of(vars)`.
#> Caused by error in `all_of()` at rlang/R/eval-tidy.R:121:3:
#> ! Can't subset elements that don't exist.
#> x Elements `Sepal.Length` and `Sepal.Width` don't exist.
Use any_of()
to allow missing variables:
starwars %>% select(any_of(vars))
#> # A tibble: 87 x 0
any_of()
is especially useful to remove variables from a data
frame because calling it again does not cause an error:
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
#> # i 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
#> # i 146 more rows
See also
The selection language page, which includes links to other selection helpers.