group_split: Split data frame by groups

Description Usage Arguments Value Grouped data frames Ungrouped data frames Rowwise data frames See Also Examples

View source: R/group_split.R

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}

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

group_keys() explains the grouping structure, by returning a data frame that has one row per group and one column per grouping variable.

Usage

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

Arguments

.tbl

A tbl

...

Grouping specification, forwarded to group_by()

.keep

Should the grouping columns be kept

Value

Grouped data frames

The primary use case for group_split() is with already grouped data frames, typically a result of group_by(). In this case group_split() only uses the first argument, the grouped tibble, and warns when ... is used.

Because some of these groups may be empty, it is best paired with group_keys() which identifies the representatives of each grouping variable for the group.

Ungrouped data frames

When used on ungrouped data frames, group_split() and group_keys() forwards the ... to group_by() before the split, therefore the ... are subject to the data mask.

Using these functions on an ungrouped data frame only makes sense if you need only one or the other, because otherwise the grouping algorithm is performed each time.

Rowwise data frames

group_split() returns a list of one-row tibbles is returned, and the ... are ignored and warned against

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# ----- use case 1 : on an already grouped tibble
ir <- iris %>%
  group_by(Species)

group_split(ir)
group_keys(ir)

# this can be useful if the grouped data has been altered before the split
ir <- iris %>%
  group_by(Species) %>%
  filter(Sepal.Length > mean(Sepal.Length))

group_split(ir)
group_keys(ir)

# ----- use case 2: using a group_by() grouping specification

# both group_split() and group_keys() have to perform the grouping
# so it only makes sense to do this if you only need one or the other
iris %>%
  group_split(Species)

iris %>%
  group_keys(Species)

javifar/TIDYVERSE-DPLYR documentation built on Dec. 20, 2021, 9:08 p.m.