This function will compute group-mean-centered scores, and then z-scored the group-mean-centered scores with respect to the grand mean.

z_scored_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

the grouping variable. If you need to pass multiple group variables, try to use quos(). Passing multiple group variables is not tested.

keep_original

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

Value

return a dataframe with a group-mean centered columns that are z-scored with respect to the grand mean.

Examples

z_scored_group_mean(iris, dplyr::ends_with("Petal.Width"), "Species")
#> # A tibble: 150 × 6
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width_group_z Species Petal.Width
#>           <dbl>       <dbl>        <dbl>               <dbl> <fct>         <dbl>
#>  1          5.1         3.5          1.4              -0.226 setosa          0.2
#>  2          4.9         3            1.4              -0.226 setosa          0.2
#>  3          4.7         3.2          1.3              -0.226 setosa          0.2
#>  4          4.6         3.1          1.5              -0.226 setosa          0.2
#>  5          5           3.6          1.4              -0.226 setosa          0.2
#>  6          5.4         3.9          1.7               0.758 setosa          0.4
#>  7          4.6         3.4          1.4               0.266 setosa          0.3
#>  8          5           3.4          1.5              -0.226 setosa          0.2
#>  9          4.4         2.9          1.4              -0.226 setosa          0.2
#> 10          4.9         3.1          1.5              -0.718 setosa          0.1
#> # ℹ 140 more rows