Mean center a variable

center(x, min_valid, max_na, grouping)

Arguments

x

A vector

min_valid

Minimal number of valid values that is required for calculation. A value between 0 and 1 indicates a proportion of values (e.g., 0.5 = 50 percent of values have to be valid).

max_na

Maximum number of NAs that are allowed before returning NA. A value between 0 and 1 indicates a proportion of values (e.g., 0.5 = 50 percent NAs are allowed).

grouping

A variable with group values for calculating grouped centers.

Examples

center(c(1:10, NA, NaN))
#>  [1] -4.5 -3.5 -2.5 -1.5 -0.5  0.5  1.5  2.5  3.5  4.5   NA  NaN
center(mtcars$mpg, grouping = mtcars$cyl)
#>  [1]  1.25714286  1.25714286 -3.86363636  1.65714286  3.60000000 -1.64285714
#>  [7] -0.80000000 -2.26363636 -3.86363636 -0.54285714 -1.94285714  1.30000000
#> [13]  2.20000000  0.10000000 -4.70000000 -4.70000000 -0.40000000  5.73636364
#> [19]  3.73636364  7.23636364 -5.16363636  0.40000000  0.10000000 -1.80000000
#> [25]  4.10000000  0.63636364 -0.66363636  3.73636364  0.70000000 -0.04285714
#> [31] -0.10000000 -5.26363636