Centers numeric variables by subtracting the mean. Supports group-wise centering for multilevel/nested data. Works with dplyr mutate/across.
Examples
if (FALSE) { # \dontrun{
# Simple mean centering
df <- center(mtcars, Vars = c("mpg", "hp"))
# Group-mean centering
df <- center(mtcars, Vars = "mpg", group_by = "cyl")
# With dplyr
library(dplyr)
mtcars %>% mutate(mpg_c = center_vec(mpg))
mtcars %>% mutate(across(c(mpg, hp), center_vec, .names = "{.col}_c"))
# Group-wise with dplyr
mtcars %>% group_by(cyl) %>% mutate(mpg_c = center_vec(mpg))
} # }