group_split: Split data frame by groups

View source: R/group-split.R

group_splitR Documentation

Split data frame by groups

Description

[Experimental]

group_split() works like base::split() but:

  • It uses the grouping structure from group_by() and therefore is subject to the data mask

  • It does not name the elements of the list based on the grouping as this only works well for a single character grouping variable. Instead, use group_keys() to access a data frame that defines the groups.

group_split() is primarily designed to work with grouped data frames. You can pass ... to group and split an ungrouped data frame, but this is generally not very useful as you want have easy access to the group metadata.

Usage

group_split(.tbl, ..., .keep = TRUE)

Arguments

.tbl

A tbl.

...

If .tbl is an ungrouped data frame, a grouping specification, forwarded to group_by().

.keep

Should the grouping columns be kept?

Value

A list of tibbles. Each tibble contains the rows of .tbl for the associated group and all the columns, including the grouping variables. Note that this returns a list_of which is slightly stricter than a simple list but is useful for representing lists where every element has the same type.

Lifecycle

group_split() is not stable because you can achieve very similar results by manipulating the nested column returned from tidyr::nest(.by =). That also retains the group keys all within a single data structure. group_split() may be deprecated in the future.

See Also

Other grouping functions: group_by(), group_map(), group_nest(), group_trim()

Examples

ir <- iris %>% group_by(Species)

group_split(ir)
group_keys(ir)

hadley/dplyr documentation built on Feb. 16, 2024, 8:27 p.m.