This function mean centers a variable, optionally within groups. It also allows to set minimal valid values or maximal allowed NAs for the calculation. If the conditions are not met, NA values are returned.

center(x, min_valid, max_na, grouping)

Arguments

x

A vector of numeric values to be mean centered.

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.

Value

A vector with mean centered values

Details

If both min_valid and max_na are provided, both conditions have to be met for the calculation to proceed. If grouping is provided, the mean centering is done within the groups defined by grouping. Missing values (NAs) are ignored in the calculation of means. If the conditions for min_valid or max_na are not met, NA values are returned for all elements of x.

Author

Juergen Wilbert

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
center(c(1:5, NA, NA), min_valid = 4)
#> [1] -2 -1  0  1  2 NA NA
center(c(1:5, NA, NA), max_na = 1)
#> [1] NA NA NA NA NA NA NA
center(c(1:5, NA, NA), min_valid = 0.7)
#> [1] -2 -1  0  1  2 NA NA
center(c(1:5, NA, NA), max_na = 0.3)
#> [1] -2 -1  0  1  2 NA NA
center(c(1:5, NA, NA), min_valid = 0.7, max_na = 1)
#> [1] NA NA NA NA NA NA NA
center(c(1:5, NA, NA), min_valid = 4, max_na = 0)
#> [1] NA NA NA NA NA NA NA