| pivot_grps {groupr} | R Documentation |
Pivot with Inapplicable Groups
Description
Pivot a dataset by defining the way the current grouping will be transformed into a new one. A pivot to wider consumes a row grouping (created by group_by2) and produces a new set of columns. A pivot to longer consumes a column grouping and produces a new row grouping.
Usage
pivot_grps(x, rows = NULL, cols = NULL)
Arguments
x |
A data frame |
rows |
A list of character vectors, defining the new row grouping |
cols |
A character vector, defining the new columns |
Details
To pivot a column grouping to a row grouping, pass the specification of the
new row grouping using the cols argument. The format is list(values_col =
"oldcol_1", "oldcol_2", ...). This will take all the data from the old columns,
combine them into a new column values_col, and automatically provide
a grouping variable, which will be called name. The values of name
will be the corresponding names of the old columns.
To pivot a row grouping to a column grouping, pass a grouped dataset (using group_by2) and specify which grouping variable should be consumed to produce a set of new columns.
Both arguments can be passed in one call, in which case rows will be handled
first, followed by cols.
See the introduction vignette for more details and examples.
Value
A pivoted data frame with the new grouping