Package tidyselect version 1.2.0
Parameter
all_of(x)
参数【x】:表示名称的字符型或表示索引位置的数值型向量。
Tip
all_of() 用于严格选择。如果字符向量中缺少任何一个变量,就会出错。
all_of() 用于配合类似 dplyr::select() 的函数使用,以便达到在数据集中按照列名获取子集的效果。
Example
library(tidyverse)
# For better printing
iris <- as_tibble(iris)
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
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
starwars %>% select(all_of(vars))
#> Error:
#> ! Problem while evaluating `all_of(vars)`.
#> Caused by error in `all_of()`:
#> ! Can't subset elements that don't exist.
#> x Elements `Sepal.Length` and `Sepal.Width` don't exist.