This function extends dplyr::select() by allowing the dynamic addition of one or more grouping variables (var_group) to the selection.

select_plus(.data, ..., var_group = NULL)

Arguments

.data

A data frame.

...

Columns to select (as in dplyr::select()).

var_group

A character string or vector of column names to additionally include, typically one or more grouping variables. Can be NULL.

Value

A data frame with the selected columns, including var_group if specified.

Details

It is especially useful when switching between an ungrouped analysis (e.g., all observations together) and a grouped analysis (e.g., stratified or including interaction terms), without rewriting code.

For instance, this allows you to write a single analysis command for both the RDD (Rapport de Démarrage des Données) and the final report, simply by changing the .qmd file, without modifying the core analysis code.

Examples

library(dplyr)
df <- tibble(x = 1:3, y = 4:6, z = 7:9)

# Simple selection
select_plus(df, x, y)
#> # A tibble: 3 × 2
#>       x     y
#>   <int> <int>
#> 1     1     4
#> 2     2     5
#> 3     3     6

# Selection with grouping variable
select_plus(df, x, var_group = "z")
#> # A tibble: 3 × 2
#>       x     z
#>   <int> <int>
#> 1     1     7
#> 2     2     8
#> 3     3     9