| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/helpers.R |
| 3 |
\name{language} |
| 4 |
\alias{language} |
| 5 |
\alias{select_helpers} |
| 6 |
\title{Selection language} |
| 7 |
\description{ |
| 8 |
\subsection{Overview of selection features:}{ |
| 9 |
|
| 10 |
tidyselect implements a DSL for selecting variables. It provides helpers |
| 11 |
for selecting variables: |
| 12 |
\itemize{ |
| 13 |
\item \code{var1:var10}: variables lying between \code{var1} on the left and \code{var10} on the right. |
| 14 |
} |
| 15 |
\itemize{ |
| 16 |
\item \code{\link[=starts_with]{starts_with("a")}}: names that start with \code{"a"}. |
| 17 |
\item \code{\link[=ends_with]{ends_with("z")}}: names that end with \code{"z"}. |
| 18 |
\item \code{\link[=contains]{contains("b")}}: names that contain \code{"b"}. |
| 19 |
\item \code{\link[=matches]{matches("x.y")}}: names that match regular expression \code{x.y}. |
| 20 |
\item \code{\link[=num_range]{num_range(x, 1:4)}}: names following the pattern, \code{x1}, \code{x2}, ..., \code{x4}. |
| 21 |
\item \code{\link[=all_of]{all_of(vars)}}/\code{\link[=any_of]{any_of(vars)}}: |
| 22 |
matches names stored in the character vector \code{vars}. \code{all_of(vars)} will |
| 23 |
error if the variables aren't present; \code{any_of(var)} will match just the |
| 24 |
variables that exist. |
| 25 |
\item \code{\link[=everything]{everything()}}: all variables. |
| 26 |
\item \code{\link[=last_col]{last_col()}}: furthest column on the right. |
| 27 |
\item \code{\link[=where]{where(is.numeric)}}: all variables where |
| 28 |
\code{is.numeric()} returns \code{TRUE}. |
| 29 |
} |
| 30 |
|
| 31 |
As well as operators for combining those selections: |
| 32 |
\itemize{ |
| 33 |
\item \code{!selection}: only variables that don't match \code{selection}. |
| 34 |
\item \code{selection1 & selection2}: only variables included in both \code{selection1} and \code{selection2}. |
| 35 |
\item \code{selection1 | selection2}: all variables that match either \code{selection1} or \code{selection2}. |
| 36 |
} |
| 37 |
|
| 38 |
When writing code inside packages you can substitute \code{"var"} for \code{var} to avoid \verb{R CMD check} notes. |
| 39 |
} |
| 40 |
} |
| 41 |
\section{Simple examples}{ |
| 42 |
|
| 43 |
|
| 44 |
Here we show the usage for the basic selection operators. See the |
| 45 |
specific help pages to learn about helpers like \code{\link[=starts_with]{starts_with()}}. |
| 46 |
|
| 47 |
The selection language can be used in functions like |
| 48 |
\code{dplyr::select()} or \code{tidyr::pivot_longer()}. Let's first attach |
| 49 |
the tidyverse: |
| 50 |
|
| 51 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{library(tidyverse) |
| 52 |
|
| 53 |
# For better printing |
| 54 |
iris <- as_tibble(iris) |
| 55 |
}\if{html}{\out{</div>}} |
| 56 |
|
| 57 |
Select variables by name: |
| 58 |
|
| 59 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(height) |
| 60 |
#> # A tibble: 87 x 1 |
| 61 |
#> height |
| 62 |
#> <int> |
| 63 |
#> 1 172 |
| 64 |
#> 2 167 |
| 65 |
#> 3 96 |
| 66 |
#> 4 202 |
| 67 |
#> # i 83 more rows |
| 68 |
|
| 69 |
iris \%>\% pivot_longer(Sepal.Length) |
| 70 |
#> # A tibble: 150 x 6 |
| 71 |
#> Sepal.Width Petal.Length Petal.Width Species name value |
| 72 |
#> <dbl> <dbl> <dbl> <fct> <chr> <dbl> |
| 73 |
#> 1 3.5 1.4 0.2 setosa Sepal.Length 5.1 |
| 74 |
#> 2 3 1.4 0.2 setosa Sepal.Length 4.9 |
| 75 |
#> 3 3.2 1.3 0.2 setosa Sepal.Length 4.7 |
| 76 |
#> 4 3.1 1.5 0.2 setosa Sepal.Length 4.6 |
| 77 |
#> # i 146 more rows |
| 78 |
}\if{html}{\out{</div>}} |
| 79 |
|
| 80 |
Select multiple variables by separating them with commas. Note how |
| 81 |
the order of columns is determined by the order of inputs: |
| 82 |
|
| 83 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(homeworld, height, mass) |
| 84 |
#> # A tibble: 87 x 3 |
| 85 |
#> homeworld height mass |
| 86 |
#> <chr> <int> <dbl> |
| 87 |
#> 1 Tatooine 172 77 |
| 88 |
#> 2 Tatooine 167 75 |
| 89 |
#> 3 Naboo 96 32 |
| 90 |
#> 4 Tatooine 202 136 |
| 91 |
#> # i 83 more rows |
| 92 |
}\if{html}{\out{</div>}} |
| 93 |
|
| 94 |
Functions like \code{tidyr::pivot_longer()} don't take variables with |
| 95 |
dots. In this case use \code{c()} to select multiple variables: |
| 96 |
|
| 97 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% pivot_longer(c(Sepal.Length, Petal.Length)) |
| 98 |
#> # A tibble: 300 x 5 |
| 99 |
#> Sepal.Width Petal.Width Species name value |
| 100 |
#> <dbl> <dbl> <fct> <chr> <dbl> |
| 101 |
#> 1 3.5 0.2 setosa Sepal.Length 5.1 |
| 102 |
#> 2 3.5 0.2 setosa Petal.Length 1.4 |
| 103 |
#> 3 3 0.2 setosa Sepal.Length 4.9 |
| 104 |
#> 4 3 0.2 setosa Petal.Length 1.4 |
| 105 |
#> # i 296 more rows |
| 106 |
}\if{html}{\out{</div>}} |
| 107 |
\subsection{Operators:}{ |
| 108 |
|
| 109 |
The \code{:} operator selects a range of consecutive variables: |
| 110 |
|
| 111 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(name:mass) |
| 112 |
#> # A tibble: 87 x 3 |
| 113 |
#> name height mass |
| 114 |
#> <chr> <int> <dbl> |
| 115 |
#> 1 Luke Skywalker 172 77 |
| 116 |
#> 2 C-3PO 167 75 |
| 117 |
#> 3 R2-D2 96 32 |
| 118 |
#> 4 Darth Vader 202 136 |
| 119 |
#> # i 83 more rows |
| 120 |
}\if{html}{\out{</div>}} |
| 121 |
|
| 122 |
The \code{!} operator negates a selection: |
| 123 |
|
| 124 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{starwars \%>\% select(!(name:mass)) |
| 125 |
#> # A tibble: 87 x 11 |
| 126 |
#> hair_color skin_color eye_color birth_year sex gender homeworld species |
| 127 |
#> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> |
| 128 |
#> 1 blond fair blue 19 male masculine Tatooine Human |
| 129 |
#> 2 <NA> gold yellow 112 none masculine Tatooine Droid |
| 130 |
#> 3 <NA> white, blue red 33 none masculine Naboo Droid |
| 131 |
#> 4 none white yellow 41.9 male masculine Tatooine Human |
| 132 |
#> # i 83 more rows |
| 133 |
#> # i 3 more variables: films <list>, vehicles <list>, starships <list> |
| 134 |
|
| 135 |
iris \%>\% select(!c(Sepal.Length, Petal.Length)) |
| 136 |
#> # A tibble: 150 x 3 |
| 137 |
#> Sepal.Width Petal.Width Species |
| 138 |
#> <dbl> <dbl> <fct> |
| 139 |
#> 1 3.5 0.2 setosa |
| 140 |
#> 2 3 0.2 setosa |
| 141 |
#> 3 3.2 0.2 setosa |
| 142 |
#> 4 3.1 0.2 setosa |
| 143 |
#> # i 146 more rows |
| 144 |
|
| 145 |
iris \%>\% select(!ends_with("Width")) |
| 146 |
#> # A tibble: 150 x 3 |
| 147 |
#> Sepal.Length Petal.Length Species |
| 148 |
#> <dbl> <dbl> <fct> |
| 149 |
#> 1 5.1 1.4 setosa |
| 150 |
#> 2 4.9 1.4 setosa |
| 151 |
#> 3 4.7 1.3 setosa |
| 152 |
#> 4 4.6 1.5 setosa |
| 153 |
#> # i 146 more rows |
| 154 |
}\if{html}{\out{</div>}} |
| 155 |
|
| 156 |
\code{&} and \code{|} take the intersection or the union of two selections: |
| 157 |
|
| 158 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(starts_with("Petal") & ends_with("Width")) |
| 159 |
#> # A tibble: 150 x 1 |
| 160 |
#> Petal.Width |
| 161 |
#> <dbl> |
| 162 |
#> 1 0.2 |
| 163 |
#> 2 0.2 |
| 164 |
#> 3 0.2 |
| 165 |
#> 4 0.2 |
| 166 |
#> # i 146 more rows |
| 167 |
|
| 168 |
iris \%>\% select(starts_with("Petal") | ends_with("Width")) |
| 169 |
#> # A tibble: 150 x 3 |
| 170 |
#> Petal.Length Petal.Width Sepal.Width |
| 171 |
#> <dbl> <dbl> <dbl> |
| 172 |
#> 1 1.4 0.2 3.5 |
| 173 |
#> 2 1.4 0.2 3 |
| 174 |
#> 3 1.3 0.2 3.2 |
| 175 |
#> 4 1.5 0.2 3.1 |
| 176 |
#> # i 146 more rows |
| 177 |
}\if{html}{\out{</div>}} |
| 178 |
|
| 179 |
To take the difference between two selections, combine the \code{&} and |
| 180 |
\code{!} operators: |
| 181 |
|
| 182 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(starts_with("Petal") & !ends_with("Width")) |
| 183 |
#> # A tibble: 150 x 1 |
| 184 |
#> Petal.Length |
| 185 |
#> <dbl> |
| 186 |
#> 1 1.4 |
| 187 |
#> 2 1.4 |
| 188 |
#> 3 1.3 |
| 189 |
#> 4 1.5 |
| 190 |
#> # i 146 more rows |
| 191 |
}\if{html}{\out{</div>}} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
\section{Details}{ |
| 196 |
|
| 197 |
The order of selected columns is determined by the inputs. |
| 198 |
\itemize{ |
| 199 |
\item \code{all_of(c("foo", "bar"))} selects \code{"foo"} first. |
| 200 |
\item \code{c(starts_with("c"), starts_with("d"))} selects all columns |
| 201 |
starting with \code{"c"} first, then all columns starting with \code{"d"}. |
| 202 |
} |
| 203 |
} |
| 204 |
|