Functions to label data frame rows by one or multiple grouping variables.

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

df_label_value(data, ..., vars = NULL, label_col = "label", sep = ", ")

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".

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 modified data frame with a column containing row labels.

Functions

  • df_label_both(): Displays both the variable name and the factor value.

  • df_label_value(): Displays only the value of a factor.

Examples

# Data preparation
df <- head(ToothGrowth)

# Labelling: Non standard evaluation
df %>%
  df_label_both(dose, supp)
#>    len supp dose             label
#> 1  4.2   VC  0.5 dose:0.5, supp:VC
#> 2 11.5   VC  0.5 dose:0.5, supp:VC
#> 3  7.3   VC  0.5 dose:0.5, supp:VC
#> 4  5.8   VC  0.5 dose:0.5, supp:VC
#> 5  6.4   VC  0.5 dose:0.5, supp:VC
#> 6 10.0   VC  0.5 dose:0.5, supp:VC

# Standard evaluation
df %>%
  df_label_both(dose, supp)
#>    len supp dose             label
#> 1  4.2   VC  0.5 dose:0.5, supp:VC
#> 2 11.5   VC  0.5 dose:0.5, supp:VC
#> 3  7.3   VC  0.5 dose:0.5, supp:VC
#> 4  5.8   VC  0.5 dose:0.5, supp:VC
#> 5  6.4   VC  0.5 dose:0.5, supp:VC
#> 6 10.0   VC  0.5 dose:0.5, supp:VC

# Nesting the data then label each subset by groups
ToothGrowth %>%
  df_nest_by(dose, supp) %>%
  df_label_both(supp, dose)
#> # A tibble: 6 × 4
#>   supp   dose data              label            
#>   <fct> <dbl> <list>            <fct>            
#> 1 VC      0.5 <tibble [10 × 1]> supp:OJ, dose:0.5
#> 2 VC      1   <tibble [10 × 1]> supp:OJ, dose:1  
#> 3 VC      2   <tibble [10 × 1]> supp:OJ, dose:2  
#> 4 OJ      0.5 <tibble [10 × 1]> supp:VC, dose:0.5
#> 5 OJ      1   <tibble [10 × 1]> supp:VC, dose:1  
#> 6 OJ      2   <tibble [10 × 1]> supp:VC, dose:2