File: with_groups.Rd

package info (click to toggle)
r-cran-dplyr 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,476 kB
  • sloc: cpp: 1,266; sh: 16; makefile: 9
file content (45 lines) | stat: -rw-r--r-- 1,426 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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/groups-with.R
\name{with_groups}
\alias{with_groups}
\title{Perform an operation with temporary groups}
\usage{
with_groups(.data, .groups, .f, ...)
}
\arguments{
\item{.data}{A data frame}

\item{.groups}{<\code{\link[=dplyr_tidy_select]{tidy-select}}> One or more variables
to group by. Unlike \code{\link[=group_by]{group_by()}}, you can only group by existing variables,
and you can use tidy-select syntax like \code{c(x, y, z)} to select multiple
variables.

Use \code{NULL} to temporarily \strong{un}group.}

\item{.f}{Function to apply to regrouped data.
Supports purrr-style \code{~} syntax}

\item{...}{Additional arguments passed on to \code{...}.}
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}

This is an experimental new function that allows you to modify the grouping
variables for a single operation.
}
\examples{
df <- tibble(g = c(1, 1, 2, 2, 3), x = runif(5))
df \%>\%
  with_groups(g, mutate, x_mean = mean(x))
df \%>\%
  with_groups(g, ~ mutate(.x, x1 = first(x)))

df \%>\%
  group_by(g) \%>\%
  with_groups(NULL, mutate, x_mean = mean(x))

# NB: grouping can't be restored if you remove the grouping variables
df \%>\%
  group_by(g) \%>\%
  with_groups(NULL, mutate, g = NULL)
}