Center and scale a set of variables. Other columns are passed through.

center_scale(d, center, scale)

Arguments

d

data.frame to work with

center

named vector of variables to center

scale

named vector of variables to scale

Value

d with centered and scaled columns altered

Examples

d <- data.frame(x = 1:5, y = c('a', 'a', 'b', 'b', 'b')) vars_to_transform = "x" t <- base::scale(as.matrix(d[, vars_to_transform, drop = FALSE]), center = TRUE, scale = TRUE) t
#> x #> [1,] -1.2649111 #> [2,] -0.6324555 #> [3,] 0.0000000 #> [4,] 0.6324555 #> [5,] 1.2649111 #> attr(,"scaled:center") #> x #> 3 #> attr(,"scaled:scale") #> x #> 1.581139
centering <- attr(t, "scaled:center") scaling <- attr(t, "scaled:scale") center_scale(d, center = centering, scale = scaling)
#> x y #> 1 -1.2649111 a #> 2 -0.6324555 a #> 3 0.0000000 b #> 4 0.6324555 b #> 5 1.2649111 b