|
38 | 38 | #' \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("select")}.
|
39 | 39 | #'
|
40 | 40 | #' @section Examples:
|
41 |
| -#' ```{r, child = "man/rmd/setup.Rmd"} |
42 |
| -#' ``` |
43 |
| -#' |
44 |
| -#' Here we show the usage for the basic selection operators. See the |
45 |
| -#' specific help pages to learn about helpers like [starts_with()]. |
46 |
| -#' |
47 |
| -#' The selection language can be used in functions like |
48 |
| -#' `dplyr::select()` or `tidyr::pivot_longer()`. Let's first attach |
49 |
| -#' the tidyverse: |
50 |
| -#' |
51 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
52 |
| -#' library(tidyverse) |
53 |
| -#' |
54 |
| -#' # For better printing |
55 |
| -#' iris <- as_tibble(iris) |
56 |
| -#' ``` |
57 |
| -#' |
58 |
| -#' Select variables by name: |
59 |
| -#' |
60 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
61 |
| -#' starwars %>% select(height) |
62 |
| -#' |
63 |
| -#' iris %>% pivot_longer(Sepal.Length) |
64 |
| -#' ``` |
65 |
| -#' |
66 |
| -#' Select multiple variables by separating them with commas. Note how |
67 |
| -#' the order of columns is determined by the order of inputs: |
68 |
| -#' |
69 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
70 |
| -#' starwars %>% select(homeworld, height, mass) |
71 |
| -#' ``` |
72 |
| -#' |
73 |
| -#' Functions like `tidyr::pivot_longer()` don't take variables with |
74 |
| -#' dots. In this case use `c()` to select multiple variables: |
75 |
| -#' |
76 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
77 |
| -#' iris %>% pivot_longer(c(Sepal.Length, Petal.Length)) |
78 |
| -#' ``` |
79 |
| -#' |
80 |
| -#' ## Operators: |
81 |
| -#' |
82 |
| -#' The `:` operator selects a range of consecutive variables: |
83 |
| -#' |
84 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
85 |
| -#' starwars %>% select(name:mass) |
86 |
| -#' ``` |
87 |
| -#' |
88 |
| -#' The `!` operator negates a selection: |
89 |
| -#' |
90 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
91 |
| -#' starwars %>% select(!(name:mass)) |
92 |
| -#' |
93 |
| -#' iris %>% select(!c(Sepal.Length, Petal.Length)) |
94 |
| -#' |
95 |
| -#' iris %>% select(!ends_with("Width")) |
96 |
| -#' ``` |
97 |
| -#' |
98 |
| -#' `&` and `|` take the intersection or the union of two selections: |
99 |
| -#' |
100 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
101 |
| -#' iris %>% select(starts_with("Petal") & ends_with("Width")) |
102 |
| -#' |
103 |
| -#' iris %>% select(starts_with("Petal") | ends_with("Width")) |
104 |
| -#' ``` |
105 |
| -#' |
106 |
| -#' To take the difference between two selections, combine the `&` and |
107 |
| -#' `!` operators: |
108 | 41 | #'
|
109 |
| -#' ```{r, comment = "#>", collapse = TRUE} |
110 |
| -#' iris %>% select(starts_with("Petal") & !ends_with("Width")) |
| 42 | +#' ```{r, echo = FALSE, results = "asis"} |
| 43 | +#' result <- rlang::with_options( |
| 44 | +#' knitr::knit_child("man/rmd/select.Rmd"), |
| 45 | +#' tibble.print_min = 4, |
| 46 | +#' tibble.max_extra_cols = 8, |
| 47 | +#' digits = 2, |
| 48 | +#' crayon.enabled = FALSE, |
| 49 | +#' cli.unicode = FALSE, |
| 50 | +#' width = 80 |
| 51 | +#' ) |
| 52 | +#' cat(result, sep = "\n") |
111 | 53 | #' ```
|
112 | 54 | #'
|
113 | 55 | #' @family single table verbs
|
|
0 commit comments