This function standardises a vector x to have a specified mean m and standard deviation sd. It can also perform grouped standardisation based on a grouping variable. Additionally, it allows for setting minimal valid values or maximal allowed NAs for the calculation. If the conditions are not met, NA values are returned.

standardise(x, grouping, min_valid, max_na, m = 0, sd = 1)

Arguments

x

A vector

grouping

A variable with group values for calculating grouped centered.

min_valid

Minimal number of valid values that is requiered 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).

m

Mean after standardisation (default = 0)

sd

Mean after standardisation (default = 1)

Value

A vector with standardised 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 standardisation is done within the groups defined by grouping. Missing values (NAs) are ignored in the calculation of means and standard deviations. 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

standardise( c(1:9, NA, NA, NA, NA, NA), m = 100, sd = 15)
#>  [1]  78.09110  83.56832  89.04555  94.52277 100.00000 105.47723 110.95445
#>  [8] 116.43168 121.90890        NA        NA        NA        NA        NA
standardise( c(1:95, NA, NA, NA, NA, NA), min_valid = 0.96)
#>   [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#>  [26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#>  [51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#>  [76] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA