Debian Codegraph @

man/everything.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/helpers-misc.R
3 \name{everything}
4 \alias{everything}
5 \alias{last_col}
6 \title{Select all variables or the last variable}
7 \usage{
8 everything(vars = NULL)
9
10 last_col(offset = 0L, vars = NULL)
11 }
12 \arguments{
13 \item{vars}{A character vector of variable names. If not supplied,
14 the variables are taken from the current selection context (as
15 established by functions like \code{select()} or \code{pivot_longer()}).}
16
17 \item{offset}{Set it to \code{n} to select the nth var from the end.}
18 }
19 \description{
20 These functions are \link[=language]{selection helpers}.
21 \itemize{
22 \item \code{\link[=everything]{everything()}} selects all variable. It is also useful in
23 combination with other tidyselect operators.
24 \item \code{\link[=last_col]{last_col()}} selects the last variable.
25 }
26 }
27 \section{Examples}{
28
29
30 Selection helpers can be used in functions like \code{dplyr::select()}
31 or \code{tidyr::pivot_longer()}. Let's first attach the tidyverse:
32
33 \if{html}{\out{<div class="sourceCode r">}}\preformatted{library(tidyverse)
34
35 # For better printing
36 iris <- as_tibble(iris)
37 mtcars <- as_tibble(mtcars)
38 }\if{html}{\out{</div>}}
39
40 Use \code{everything()} to select all variables:
41
42 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(everything())
43 #> # A tibble: 150 x 5
44 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
45 #> <dbl> <dbl> <dbl> <dbl> <fct>
46 #> 1 5.1 3.5 1.4 0.2 setosa
47 #> 2 4.9 3 1.4 0.2 setosa
48 #> 3 4.7 3.2 1.3 0.2 setosa
49 #> 4 4.6 3.1 1.5 0.2 setosa
50 #> # i 146 more rows
51
52 mtcars \%>\% pivot_longer(everything())
53 #> # A tibble: 352 x 2
54 #> name value
55 #> <chr> <dbl>
56 #> 1 mpg 21
57 #> 2 cyl 6
58 #> 3 disp 160
59 #> 4 hp 110
60 #> # i 348 more rows
61 }\if{html}{\out{</div>}}
62
63 Use \code{last_col()} to select the last variable:
64
65 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(last_col())
66 #> # A tibble: 150 x 1
67 #> Species
68 #> <fct>
69 #> 1 setosa
70 #> 2 setosa
71 #> 3 setosa
72 #> 4 setosa
73 #> # i 146 more rows
74
75 mtcars \%>\% pivot_longer(last_col())
76 #> # A tibble: 32 x 12
77 #> mpg cyl disp hp drat wt qsec vs am gear name value
78 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
79 #> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 carb 4
80 #> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 carb 4
81 #> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 carb 1
82 #> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 carb 1
83 #> # i 28 more rows
84 }\if{html}{\out{</div>}}
85
86 Supply an offset \code{n} to select a variable located \code{n} positions
87 from the end:
88
89 \if{html}{\out{<div class="sourceCode r">}}\preformatted{mtcars \%>\% select(1:last_col(5))
90 #> # A tibble: 32 x 6
91 #> mpg cyl disp hp drat wt
92 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
93 #> 1 21 6 160 110 3.9 2.62
94 #> 2 21 6 160 110 3.9 2.88
95 #> 3 22.8 4 108 93 3.85 2.32
96 #> 4 21.4 6 258 110 3.08 3.22
97 #> # i 28 more rows
98 }\if{html}{\out{</div>}}
99 }
100
101 \seealso{
102 The \link[=language]{selection language} page, which includes links to other selection helpers.
103 }