Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

group_mean_center

Group-mean centering


Description

Compute group-mean centered variables. Usually used for HLM level-1 predictors.

Usage

group_mean_center(
  data,
  vars,
  by,
  std = FALSE,
  add_suffix = "",
  add_group_mean = "_mean"
)

Arguments

data

Data object.

vars

Variable(s) to be centered.

by

Grouping variable.

std

Standardized or not. Default is FALSE.

add_suffix

The suffix of the centered variable(s). Default is "". You may set it to "_c", "_center", etc.

add_group_mean

The suffix of the variable name(s) of group means. Default is "_mean" (see Examples).

Value

A new data object containing the centered variable(s).

See Also

Examples

d=data.table(x=1:9, g=rep(1:3, each=3))

d.c=group_mean_center(d, "x", by="g")
d.c

d.c=group_mean_center(d, "x", by="g", add_suffix="_c")
d.c

bruceR

Broadly Useful Convenient and Efficient R Functions

v0.6.2
GPL-3
Authors
Han-Wu-Shuang Bao [aut, cre]
Initial release
2021-04-08

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.