This function will compute group-mean-centered scores.

center_group_mean(data, cols, group, keep_original = TRUE)

Arguments

data

A data.frame or a data.frame extension (e.g. a tibble).

cols

Columns that need to be centered. See `dplyr::dplyr_tidy_select` for available options.

group

character. grouping variable

keep_original

default is `TRUE`. Set to `FALSE` to remove original columns

Value

An object of the same type as .data. The output has the following properties: 1. Columns from .data will be preserved 2. Columns with scores that are group-mean centered

Examples

center_group_mean(iris,where(is.numeric), group = Species)
#> # A tibble: 150 × 9
#>    Sepal.Length_group_c Sepal.Width_group_c Petal.Length_group_c
#>                   <dbl>               <dbl>                <dbl>
#>  1              0.0940               0.0720              -0.0620
#>  2             -0.106               -0.428               -0.0620
#>  3             -0.306               -0.228               -0.162 
#>  4             -0.406               -0.328                0.0380
#>  5             -0.00600              0.172               -0.0620
#>  6              0.394                0.472                0.238 
#>  7             -0.406               -0.0280              -0.0620
#>  8             -0.00600             -0.0280               0.0380
#>  9             -0.606               -0.528               -0.0620
#> 10             -0.106               -0.328                0.0380
#> # ℹ 140 more rows
#> # ℹ 6 more variables: Petal.Width_group_c <dbl>, Species <fct>,
#> #   Sepal.Length <dbl>, Sepal.Width <dbl>, Petal.Length <dbl>,
#> #   Petal.Width <dbl>