center_variable | R Documentation |
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.
center_variable(variable, scale = FALSE, by = NULL)
variable |
A numeric vector. |
scale |
A logical vector. If |
by |
A vector with the |
A numeric vector.
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.