Select variables from character vectors
These selection helpers select variables contained in a character vector. They are especially useful for programming with selecting functions.
The order of selected columns is determined by the order in the vector.
all_of(x) any_of(x, ..., vars = NULL)
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 |
Selection helpers can be used in functions like dplyr::select()
or tidyr::pivot_longer()
. Let's first attach the tidyverse:
library(tidyverse) # For better printing iris <- as_tibble(iris)
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 #> # ... with 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 #> # ... 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
If any of the variable is missing from the data frame, that's an error:
starwars %>% select(all_of(vars))
## Error: Can't subset columns that don't exist. ## x Columns `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 #> # ... 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
The selection language page, which includes links to other selection helpers.
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.