https://github.com/hadley/dplyr
Raw File
Tip revision: 39ee11bfe78c4a301070b00d3b92127217786ba7 authored by Hadley Wickham on 23 January 2020, 16:18:08 UTC
Preserve drop attr
Tip revision: 39ee11b
group_nest.Rd
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/group_nest.R
\name{group_nest}
\alias{group_nest}
\title{Nest a tibble using a grouping specification}
\usage{
group_nest(.tbl, ..., .key = "data", keep = FALSE)
}
\arguments{
\item{.tbl}{A tbl}

\item{...}{Grouping specification, forwarded to \code{\link[=group_by]{group_by()}}}

\item{.key}{the name of the list column}

\item{keep}{Should the grouping columns be kept in the list column.}
}
\value{
A tbl with one row per unique combination of the grouping variables.
The first columns are the grouping variables, followed by a list column of tibbles
with matching rows of the remaining columns.
}
\description{
\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}

Nest a tibble using a grouping specification
}
\section{Grouped data frames}{


The primary use case for \code{\link[=group_nest]{group_nest()}} is with already grouped data frames,
typically a result of \code{\link[=group_by]{group_by()}}. In this case \code{\link[=group_nest]{group_nest()}} only uses
the first argument, the grouped tibble, and warns when \code{...} is used.
}

\section{Ungrouped data frames}{


When used on ungrouped data frames, \code{\link[=group_nest]{group_nest()}} forwards the \code{...} to
\code{\link[=group_by]{group_by()}} before nesting, therefore the \code{...} are subject to the data mask.
}

\examples{

#----- use case 1: a grouped data frame
iris \%>\%
  group_by(Species) \%>\%
  group_nest()

# this can be useful if the grouped data has been altered before nesting
iris \%>\%
  group_by(Species) \%>\%
  filter(Sepal.Length > mean(Sepal.Length)) \%>\%
  group_nest()

#----- use case 2: using group_nest() on a ungrouped data frame with
#                  a grouping specification that uses the data mask
starwars \%>\%
  group_nest(species, homeworld)
}
\seealso{
Other grouping functions: 
\code{\link{group_by}()},
\code{\link{group_map}()},
\code{\link{group_split}()},
\code{\link{group_trim}()}
}
\concept{grouping functions}
\keyword{internal}
back to top