Debian Codegraph @

man/where.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/helpers-where.R
3 \name{where}
4 \alias{where}
5 \title{Select variables with a function}
6 \usage{
7 where(fn)
8 }
9 \arguments{
10 \item{fn}{A function that returns \code{TRUE} or \code{FALSE} (technically, a
11 \emph{predicate} function). Can also be a purrr-like formula.}
12 }
13 \description{
14 This \link[=language]{selection helper} selects the variables for which a
15 function returns \code{TRUE}.
16 }
17 \section{Examples}{
18
19
20 Selection helpers can be used in functions like \code{dplyr::select()}
21 or \code{tidyr::pivot_longer()}. Let's first attach the tidyverse:
22
23 \if{html}{\out{<div class="sourceCode r">}}\preformatted{library(tidyverse)
24
25 # For better printing
26 iris <- as_tibble(iris)
27 }\if{html}{\out{</div>}}
28
29 \code{where()} takes a function and returns all variables for which the
30 function returns \code{TRUE}:
31
32 \if{html}{\out{<div class="sourceCode r">}}\preformatted{is.factor(iris[[4]])
33 #> [1] FALSE
34
35 is.factor(iris[[5]])
36 #> [1] TRUE
37
38 iris \%>\% select(where(is.factor))
39 #> # A tibble: 150 x 1
40 #> Species
41 #> <fct>
42 #> 1 setosa
43 #> 2 setosa
44 #> 3 setosa
45 #> 4 setosa
46 #> # i 146 more rows
47
48 is.numeric(iris[[4]])
49 #> [1] TRUE
50
51 is.numeric(iris[[5]])
52 #> [1] FALSE
53
54 iris \%>\% select(where(is.numeric))
55 #> # A tibble: 150 x 4
56 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
57 #> <dbl> <dbl> <dbl> <dbl>
58 #> 1 5.1 3.5 1.4 0.2
59 #> 2 4.9 3 1.4 0.2
60 #> 3 4.7 3.2 1.3 0.2
61 #> 4 4.6 3.1 1.5 0.2
62 #> # i 146 more rows
63 }\if{html}{\out{</div>}}
64 \subsection{The formula shorthand}{
65
66 You can use purrr-like formulas as a shortcut for creating a
67 function on the spot. These expressions are equivalent:
68
69 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(where(is.numeric))
70 #> # A tibble: 150 x 4
71 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
72 #> <dbl> <dbl> <dbl> <dbl>
73 #> 1 5.1 3.5 1.4 0.2
74 #> 2 4.9 3 1.4 0.2
75 #> 3 4.7 3.2 1.3 0.2
76 #> 4 4.6 3.1 1.5 0.2
77 #> # i 146 more rows
78
79 iris \%>\% select(where(function(x) is.numeric(x)))
80 #> # A tibble: 150 x 4
81 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
82 #> <dbl> <dbl> <dbl> <dbl>
83 #> 1 5.1 3.5 1.4 0.2
84 #> 2 4.9 3 1.4 0.2
85 #> 3 4.7 3.2 1.3 0.2
86 #> 4 4.6 3.1 1.5 0.2
87 #> # i 146 more rows
88
89 iris \%>\% select(where(~ is.numeric(.x)))
90 #> # A tibble: 150 x 4
91 #> Sepal.Length Sepal.Width Petal.Length Petal.Width
92 #> <dbl> <dbl> <dbl> <dbl>
93 #> 1 5.1 3.5 1.4 0.2
94 #> 2 4.9 3 1.4 0.2
95 #> 3 4.7 3.2 1.3 0.2
96 #> 4 4.6 3.1 1.5 0.2
97 #> # i 146 more rows
98 }\if{html}{\out{</div>}}
99
100 The shorthand is useful for adding logic inline. Here we select all
101 numeric variables whose mean is greater than 3.5:
102
103 \if{html}{\out{<div class="sourceCode r">}}\preformatted{iris \%>\% select(where(~ is.numeric(.x) && mean(.x) > 3.5))
104 #> # A tibble: 150 x 2
105 #> Sepal.Length Petal.Length
106 #> <dbl> <dbl>
107 #> 1 5.1 1.4
108 #> 2 4.9 1.4
109 #> 3 4.7 1.3
110 #> 4 4.6 1.5
111 #> # i 146 more rows
112 }\if{html}{\out{</div>}}
113 }
114 }
115