center_mlm.Rd
This function will group mean centered the scores at the level 1 and create a mean score for each group at L2.
center_mlm(data, cols, group, keep_original = TRUE)
A data.frame or a data.frame extension (e.g. a tibble).
Columns that need to be centered. See `dplyr::dplyr_tidy_select` for available options.
the grouping variable. Must be character.
default is `TRUE`. Set to `FALSE` to remove original columns
An object of the same type as .data. The output has the following properties: 1. Columns from .data will be preserved 2. Columns with L1 scores that are group-mean centered. 3. Columns with L2 aggregated means.
center_mlm(iris,dplyr::ends_with('Length'),group = 'Species')
#> # A tibble: 150 × 9
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> 7 4.6 3.4 1.4 0.3 setosa
#> 8 5 3.4 1.5 0.2 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 4.9 3.1 1.5 0.1 setosa
#> # ℹ 140 more rows
#> # ℹ 4 more variables: Sepal.Length_group_c <dbl>, Petal.Length_group_c <dbl>,
#> # Sepal.Length_mean <dbl>, Petal.Length_mean <dbl>