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".
df_split_by(
data,
...,
vars = NULL,
label_col = "label",
labeller = df_label_both,
sep = c(", ", ":")
)
a data frame
One or more unquoted expressions (or variable names) separated by commas. Used as grouping variables.
a character vector containing the grouping variables of interest.
column to hold the label of the data subsets. Default column name is "label".
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()
.
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 ':'.
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.
# Split a data frame
# :::::::::::::::::::::::::::::::::::::::::::::::::
# Create a grouped data
res <- ToothGrowth %>%
df_split_by(dose, supp)
res
#> # A tibble: 6 × 4
#> supp dose data label
#> <fct> <dbl> <list> <fct>
#> 1 VC 0.5 <tibble [10 × 2]> dose:0.5, supp:OJ
#> 2 VC 1 <tibble [10 × 2]> dose:0.5, supp:VC
#> 3 VC 2 <tibble [10 × 2]> dose:1, supp:OJ
#> 4 OJ 0.5 <tibble [10 × 2]> dose:1, supp:VC
#> 5 OJ 1 <tibble [10 × 2]> dose:2, supp:OJ
#> 6 OJ 2 <tibble [10 × 2]> dose:2, supp:VC
# Show subsets
res$data
#> [[1]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 4.2 dose:0.5, supp:OJ
#> 2 11.5 dose:0.5, supp:OJ
#> 3 7.3 dose:0.5, supp:OJ
#> 4 5.8 dose:0.5, supp:OJ
#> 5 6.4 dose:0.5, supp:OJ
#> 6 10 dose:0.5, supp:OJ
#> 7 11.2 dose:0.5, supp:OJ
#> 8 11.2 dose:0.5, supp:OJ
#> 9 5.2 dose:0.5, supp:OJ
#> 10 7 dose:0.5, supp:OJ
#>
#> [[2]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 16.5 dose:0.5, supp:VC
#> 2 16.5 dose:0.5, supp:VC
#> 3 15.2 dose:0.5, supp:VC
#> 4 17.3 dose:0.5, supp:VC
#> 5 22.5 dose:0.5, supp:VC
#> 6 17.3 dose:0.5, supp:VC
#> 7 13.6 dose:0.5, supp:VC
#> 8 14.5 dose:0.5, supp:VC
#> 9 18.8 dose:0.5, supp:VC
#> 10 15.5 dose:0.5, supp:VC
#>
#> [[3]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 23.6 dose:1, supp:OJ
#> 2 18.5 dose:1, supp:OJ
#> 3 33.9 dose:1, supp:OJ
#> 4 25.5 dose:1, supp:OJ
#> 5 26.4 dose:1, supp:OJ
#> 6 32.5 dose:1, supp:OJ
#> 7 26.7 dose:1, supp:OJ
#> 8 21.5 dose:1, supp:OJ
#> 9 23.3 dose:1, supp:OJ
#> 10 29.5 dose:1, supp:OJ
#>
#> [[4]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 15.2 dose:1, supp:VC
#> 2 21.5 dose:1, supp:VC
#> 3 17.6 dose:1, supp:VC
#> 4 9.7 dose:1, supp:VC
#> 5 14.5 dose:1, supp:VC
#> 6 10 dose:1, supp:VC
#> 7 8.2 dose:1, supp:VC
#> 8 9.4 dose:1, supp:VC
#> 9 16.5 dose:1, supp:VC
#> 10 9.7 dose:1, supp:VC
#>
#> [[5]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 19.7 dose:2, supp:OJ
#> 2 23.3 dose:2, supp:OJ
#> 3 23.6 dose:2, supp:OJ
#> 4 26.4 dose:2, supp:OJ
#> 5 20 dose:2, supp:OJ
#> 6 25.2 dose:2, supp:OJ
#> 7 25.8 dose:2, supp:OJ
#> 8 21.2 dose:2, supp:OJ
#> 9 14.5 dose:2, supp:OJ
#> 10 27.3 dose:2, supp:OJ
#>
#> [[6]]
#> # A tibble: 10 × 2
#> len label
#> <dbl> <fct>
#> 1 25.5 dose:2, supp:VC
#> 2 26.4 dose:2, supp:VC
#> 3 22.4 dose:2, supp:VC
#> 4 24.5 dose:2, supp:VC
#> 5 24.8 dose:2, supp:VC
#> 6 30.9 dose:2, supp:VC
#> 7 26.4 dose:2, supp:VC
#> 8 27.3 dose:2, supp:VC
#> 9 29.4 dose:2, supp:VC
#> 10 23 dose:2, supp:VC
#>
# Add panel/subset labels
res <- ToothGrowth %>%
df_split_by(dose, supp)
res
#> # A tibble: 6 × 4
#> supp dose data label
#> <fct> <dbl> <list> <fct>
#> 1 VC 0.5 <tibble [10 × 2]> dose:0.5, supp:OJ
#> 2 VC 1 <tibble [10 × 2]> dose:0.5, supp:VC
#> 3 VC 2 <tibble [10 × 2]> dose:1, supp:OJ
#> 4 OJ 0.5 <tibble [10 × 2]> dose:1, supp:VC
#> 5 OJ 1 <tibble [10 × 2]> dose:2, supp:OJ
#> 6 OJ 2 <tibble [10 × 2]> dose:2, supp:VC