| group_split {poorman} | R Documentation |
Split data.frame by groups
Description
group_split() works like base::split() but
it uses the grouping structure from
group_by()and is therefore subject to the data maskit does not name the elements of the list based on the grouping as this typically loses information and is confusing
Usage
group_split(.data, ..., .keep = TRUE)
group_keys(.data)
Arguments
.data |
A |
... |
Grouping specification, forwarded to |
.keep |
|
Details
Grouped data.frames:
The primary use case for group_split() is with already groups data.frames, typically a result of group_by(). In
this case, group_split() only uses the first argument, the grouped data.frame, 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() forwards the ... to group_by() before the split, therefore
the ... are subject to the data mask.
Value
-
group_split()returns a list ofdata.frames. Eachdata.framecontains the rows of.datawith the associated group and all the columns, including the grouping variables. -
group_keys()returns adata.framewith one row per group, and one column per grouping variable
See Also
Examples
# Grouped data.frames:
mtcars %>% group_by(cyl, am) %>% group_split()
mtcars %>% group_by(cyl, am) %>% group_split(.keep = FALSE)
mtcars %>% group_by(cyl, am) %>% group_keys()
# Ungrouped data.frames:
mtcars %>% group_split(am, cyl)