center_variable: Center variable

Description Usage Arguments Value Examples

View source: R/stats.R

Description

Center a variable by subtracting the mean from each element. Centering can be performed by the grand mean when by = NULL (the default), or by group means when by is a factor variable.

Usage

1
center_variable(variable, scale = FALSE, by = NULL)

Arguments

variable

A numeric vector.

scale

A logical vector. If scale = TRUE, the centered values of variable are divided by their standard deviation.

by

A vector with the factor class.

Value

A numeric vector.

Examples

1
2
3
4
5
6
7
8
df <- data.frame(
  id = 1:20,
  group = as.factor(sample(c("A", "B"), 20, replace = TRUE)),
  body_mass = rnorm(20, mean = 65, sd = 12)
)

df$body_mass_centered <- center_variable(df$body_mass, by = df$group)
df

lvmisc documentation built on April 5, 2021, 5:06 p.m.