File: pick.Rd

package info (click to toggle)
r-cran-dplyr 1.1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: cpp: 1,403; sh: 17; makefile: 7
file content (80 lines) | stat: -rw-r--r-- 2,983 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pick.R
\name{pick}
\alias{pick}
\title{Select a subset of columns}
\usage{
pick(...)
}
\arguments{
\item{...}{<\code{\link[=dplyr_tidy_select]{tidy-select}}>

Columns to pick.

You can't pick grouping columns because they are already automatically
handled by the verb (i.e. \code{\link[=summarise]{summarise()}} or \code{\link[=mutate]{mutate()}}).}
}
\value{
A tibble containing the selected columns for the current group.
}
\description{
\code{pick()} provides a way to easily select a subset of columns from your data
using \code{\link[=select]{select()}} semantics while inside a
\link[rlang:args_data_masking]{"data-masking"} function like \code{\link[=mutate]{mutate()}} or
\code{\link[=summarise]{summarise()}}. \code{pick()} returns a data frame containing the selected columns
for the current group.

\code{pick()} is complementary to \code{\link[=across]{across()}}:
\itemize{
\item With \code{pick()}, you typically apply a function to the full data frame.
\item With \code{across()}, you typically apply a function to each column.
}
}
\details{
Theoretically, \code{pick()} is intended to be replaceable with an equivalent call
to \code{tibble()}. For example, \code{pick(a, c)} could be replaced with
\code{tibble(a = a, c = c)}, and \code{pick(everything())} on a data frame with cols
\code{a}, \code{b}, and \code{c} could be replaced with \code{tibble(a = a, b = b, c = c)}.
\code{pick()} specially handles the case of an empty selection by returning a 1
row, 0 column tibble, so an exact replacement is more like:

\if{html}{\out{<div class="sourceCode">}}\preformatted{size <- vctrs::vec_size_common(..., .absent = 1L)
out <- vctrs::vec_recycle_common(..., .size = size)
tibble::new_tibble(out, nrow = size)
}\if{html}{\out{</div>}}
}
\examples{
df <- tibble(
  x = c(3, 2, 2, 2, 1),
  y = c(0, 2, 1, 1, 4),
  z1 = c("a", "a", "a", "b", "a"),
  z2 = c("c", "d", "d", "a", "c")
)
df

# `pick()` provides a way to select a subset of your columns using
# tidyselect. It returns a data frame.
df \%>\% mutate(cols = pick(x, y))

# This is useful for functions that take data frames as inputs.
# For example, you can compute a joint rank between `x` and `y`.
df \%>\% mutate(rank = dense_rank(pick(x, y)))

# `pick()` is also useful as a bridge between data-masking functions (like
# `mutate()` or `group_by()`) and functions with tidy-select behavior (like
# `select()`). For example, you can use `pick()` to create a wrapper around
# `group_by()` that takes a tidy-selection of columns to group on. For more
# bridge patterns, see
# https://rlang.r-lib.org/reference/topic-data-mask-programming.html#bridge-patterns.
my_group_by <- function(data, cols) {
  group_by(data, pick({{ cols }}))
}

df \%>\% my_group_by(c(x, starts_with("z")))

# Or you can use it to dynamically select columns to `count()` by
df \%>\% count(pick(starts_with("z")))
}
\seealso{
\code{\link[=across]{across()}}
}