Debian Codegraph @

man/starts_with.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/helpers-pattern.R
3 \name{starts_with}
4 \alias{starts_with}
5 \alias{ends_with}
6 \alias{contains}
7 \alias{matches}
8 \alias{num_range}
9 \title{Select variables that match a pattern}
10 \usage{
11 starts_with(match, ignore.case = TRUE, vars = NULL)
12
13 ends_with(match, ignore.case = TRUE, vars = NULL)
14
15 contains(match, ignore.case = TRUE, vars = NULL)
16
17 matches(match, ignore.case = TRUE, perl = FALSE, vars = NULL)
18
19 num_range(prefix, range, suffix = "", width = NULL, vars = NULL)
20 }
21 \arguments{
22 \item{match}{A character vector. If length > 1, the union of the
23 matches is taken.
24
25 For \code{starts_with()}, \code{ends_with()}, and \code{contains()} this is an exact
26 match. For \code{matches()} this is a regular expression, and can be a
27 stringr pattern.}
28
29 \item{ignore.case}{If \code{TRUE}, the default, ignores case when matching
30 names.}
31
32 \item{vars}{A character vector of variable names. If not supplied,
33 the variables are taken from the current selection context (as
34 established by functions like \code{select()} or \code{pivot_longer()}).}
35
36 \item{perl}{Should Perl-compatible regexps be used?}
37
38 \item{prefix, suffix}{A prefix/suffix added before/after the numeric range.}
39
40 \item{range}{A sequence of integers, like \code{1:5}.}
41
42 \item{width}{Optionally, the "width" of the numeric range. For example,
43 a range of 2 gives "01", a range of three "001", etc.}
44 }
45 \description{
46 These \link[=language]{selection helpers} match variables according
47 to a given pattern.
48 \itemize{
49 \item \code{\link[=starts_with]{starts_with()}}: Starts with an exact prefix.
50 \item \code{\link[=ends_with]{ends_with()}}: Ends with an exact suffix.
51 \item \code{\link[=contains]{contains()}}: Contains a literal string.
52 \item \code{\link[=matches]{matches()}}: Matches a regular expression.
53 \item \code{\link[=num_range]{num_range()}}: Matches a numerical range like x01, x02, x03.
54 }
55 }
56 \section{Examples}{
57
58
59 Selection helpers can be used in functions like \code{dplyr::select()}
60 or \code{tidyr::pivot_longer()}. Let's first attach the tidyverse:
61
62 \if{html}{\out{<div class="sourceCode r">}}\preformatted{library(tidyverse)
63
64 # For better printing
65 iris <- as_tibble(iris)
66 }\if{html}{\out{</div>}}
67
68 \code{starts_with()} selects all variables matching a prefix and
69 \code{ends_with()} matches a suffix:
70
71 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(starts_with("Sepal"))
72 #> # A tibble: 150 x 2
73 #> Sepal.Length Sepal.Width
74 #> <dbl> <dbl>
75 #> 1 5.1 3.5
76 #> 2 4.9 3
77 #> 3 4.7 3.2
78 #> 4 4.6 3.1
79 #> # i 146 more rows
80
81 iris \%>\% select(ends_with("Width"))
82 #> # A tibble: 150 x 2
83 #> Sepal.Width Petal.Width
84 #> <dbl> <dbl>
85 #> 1 3.5 0.2
86 #> 2 3 0.2
87 #> 3 3.2 0.2
88 #> 4 3.1 0.2
89 #> # i 146 more rows
90 }\if{html}{\out{</div>}}
91
92 You can supply multiple prefixes or suffixes. Note how the order of
93 variables depends on the order of the suffixes and prefixes:
94
95 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(starts_with(c("Petal", "Sepal")))
96 #> # A tibble: 150 x 4
97 #> Petal.Length Petal.Width Sepal.Length Sepal.Width
98 #> <dbl> <dbl> <dbl> <dbl>
99 #> 1 1.4 0.2 5.1 3.5
100 #> 2 1.4 0.2 4.9 3
101 #> 3 1.3 0.2 4.7 3.2
102 #> 4 1.5 0.2 4.6 3.1
103 #> # i 146 more rows
104
105 iris \%>\% select(ends_with(c("Width", "Length")))
106 #> # A tibble: 150 x 4
107 #> Sepal.Width Petal.Width Sepal.Length Petal.Length
108 #> <dbl> <dbl> <dbl> <dbl>
109 #> 1 3.5 0.2 5.1 1.4
110 #> 2 3 0.2 4.9 1.4
111 #> 3 3.2 0.2 4.7 1.3
112 #> 4 3.1 0.2 4.6 1.5
113 #> # i 146 more rows
114 }\if{html}{\out{</div>}}
115
116 \code{contains()} selects columns whose names contain a word:
117
118 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(contains("al"))
119 #> # A tibble: 150 x 4
120 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
121 #> <dbl> <dbl> <dbl> <dbl>
122 #> 1 5.1 3.5 1.4 0.2
123 #> 2 4.9 3 1.4 0.2
124 #> 3 4.7 3.2 1.3 0.2
125 #> 4 4.6 3.1 1.5 0.2
126 #> # i 146 more rows
127 }\if{html}{\out{</div>}}
128
129 \code{starts_with()}, \code{ends_with()}, and \code{contains()} do not use regular expressions. To select with a
130 regexp use \code{matches()}:
131
132 \if{html}{\out{<div class="sourceCode r">}}\preformatted{# [pt] is matched literally:
133 iris \%>\% select(contains("[pt]al"))
134 #> # A tibble: 150 x 0
135
136 # [pt] is interpreted as a regular expression
137 iris \%>\% select(matches("[pt]al"))
138 #> # A tibble: 150 x 4
139 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
140 #> <dbl> <dbl> <dbl> <dbl>
141 #> 1 5.1 3.5 1.4 0.2
142 #> 2 4.9 3 1.4 0.2
143 #> 3 4.7 3.2 1.3 0.2
144 #> 4 4.6 3.1 1.5 0.2
145 #> # i 146 more rows
146 }\if{html}{\out{</div>}}
147
148 \code{starts_with()} selects all variables starting with a prefix. To
149 select a range, use \code{num_range()}. Compare:
150
151 \if{html}{\out{<div class="sourceCode r">}}\preformatted{billboard \%>\% select(starts_with("wk"))
152 #> # A tibble: 317 x 76
153 #> wk1 wk2 wk3 wk4 wk5 wk6 wk7 wk8 wk9 wk10 wk11 wk12 wk13
154 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
155 #> 1 87 82 72 77 87 94 99 NA NA NA NA NA NA
156 #> 2 91 87 92 NA NA NA NA NA NA NA NA NA NA
157 #> 3 81 70 68 67 66 57 54 53 51 51 51 51 47
158 #> 4 76 76 72 69 67 65 55 59 62 61 61 59 61
159 #> # i 313 more rows
160 #> # i 63 more variables: wk14 <dbl>, wk15 <dbl>, wk16 <dbl>, wk17 <dbl>,
161 #> # wk18 <dbl>, wk19 <dbl>, wk20 <dbl>, wk21 <dbl>, ...
162
163 billboard \%>\% select(num_range("wk", 10:15))
164 #> # A tibble: 317 x 6
165 #> wk10 wk11 wk12 wk13 wk14 wk15
166 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
167 #> 1 NA NA NA NA NA NA
168 #> 2 NA NA NA NA NA NA
169 #> 3 51 51 51 47 44 38
170 #> 4 61 61 59 61 66 72
171 #> # i 313 more rows
172 }\if{html}{\out{</div>}}
173 }
174
175 \seealso{
176 The \link[=language]{selection language} page, which includes links to other selection helpers.
177 }