df_split_by: Split a Data Frame into Subset

View source: R/df.R

df_split_byR Documentation

Split a Data Frame into Subset

Description

Split a data frame by groups into subsets or data panel. Very similar to the function df_nest_by(). The only difference is that, it adds label to each data subset. Labels are the combination of the grouping variable levels. The column holding labels are named "label".

Usage

df_split_by(
  data,
  ...,
  vars = NULL,
  label_col = "label",
  labeller = df_label_both,
  sep = c(", ", ":")
)

Arguments

data

a data frame

...

One or more unquoted expressions (or variable names) separated by commas. Used as grouping variables.

vars

a character vector containing the grouping variables of interest.

label_col

column to hold the label of the data subsets. Default column name is "label".

labeller

A function that takes a data frame, the grouping variables, label_col and label_sep arguments, and add labels into the data frame. Example of possible values are: df_label_both() and df_label_value().

sep

String separating labelling variables and values. Should be of length 2 in the function df_label_both(). 1) One sep is used to separate groups, for example ','; 2) The other sep between group name and levels; for example ':'.

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, and a column named label, containing labels.

Examples


# Split a data frame
# :::::::::::::::::::::::::::::::::::::::::::::::::
# Create a grouped data
res <- ToothGrowth %>%
  df_split_by(dose, supp)
res

# Show subsets
res$data

# Add panel/subset labels
res <- ToothGrowth %>%
  df_split_by(dose, supp)
res

rstatix documentation built on Feb. 16, 2023, 6:10 p.m.