Winsorize: Winsorize (Replace Extreme Values by Less Extreme Ones)

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/DescTools.r

Description

Winsorizing a vector means that a predefined quantum of the smallest and/or the largest values are replaced by less extreme values. Thereby the substitute values are the most extreme retained values.

Usage

1
2
Winsorize(x, minval = NULL, maxval = NULL, probs = c(0.05, 0.95),
          na.rm = FALSE)

Arguments

x

a numeric vector to be winsorized.

minval

the low border, all values being lower than this will be replaced by this value. The default is set to the 5%-quantile of x.

maxval

the high border, all values being larger than this will be replaced by this value. The default is set to the 95%-quantile of x.

probs

numeric vector of probabilities with values in [0,1] as used in quantile.

na.rm

should NAs be omitted to calculate the quantiles?
Note that NAs in x are preserved and left unchanged anyway.

Details

The winsorized vector is obtained by

wins(x) = -c if x < -c, c if x > c, x otherwise

Consider standardizing (possibly robust) the data before winsorizing.

Value

A vector of the same length as the original data x containing the winsorized data.

Author(s)

Andri Signorell <[email protected]>

See Also

Winsorize from the package robustHD contains an option to winsorize multivariate data

scale, RobScale

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## generate data
set.seed(1234)     # for reproducibility
x <- rnorm(10)     # standard normal
x[1] <- x[1] * 10  # introduce outlier

## Winsorize data
x
Winsorize(x)

# use Large and Small, if a fix number of values should be winsorized (here k=3):
Winsorize(x, minval=tail(Small(x, k=3), 1), maxval=head(Large(x, k=3), 1))

Example output

 [1] -12.0706575   0.2774292   1.0844412  -2.3456977   0.4291247   0.5060559
 [7]  -0.5747400  -0.5466319  -0.5644520  -0.8900378
 [1] -7.6944256  0.2774292  0.8241678 -2.3456977  0.4291247  0.5060559
 [7] -0.5747400 -0.5466319 -0.5644520 -0.8900378
 [1] -0.8900378  0.2774292  0.4291247 -0.8900378  0.4291247  0.4291247
 [7] -0.5747400 -0.5466319 -0.5644520 -0.8900378

DescTools documentation built on Sept. 13, 2017, 9:04 a.m.