Winsorize data

winsorize(data, ...)

# S3 method for numeric
winsorize(data, threshold = 0.2, verbose = TRUE, ...)

Arguments

data

Dataframe or vector.

...

Currently not used.

threshold

The amount of winsorization.

verbose

Toggle warnings.

Value

A dataframe with winsorized columns or a winsorized vector.

Details

Winsorizing or winsorization is the transformation of statistics by limiting extreme values in the statistical data to reduce the effect of possibly spurious outliers. The distribution of many statistics can be heavily influenced by outliers. A typical strategy is to set all outliers (values beyond a certain threshold) to a specified percentile of the data; for example, a 90\ to the 5th percentile, and data above the 95th percentile set to the 95th percentile. Winsorized estimators are usually more robust to outliers than their more standard forms.

Examples

winsorize(iris$Sepal.Length, threshold = 0.2)
#>   [1] 5.1 5.0 5.0 5.0 5.0 5.4 5.0 5.0 5.0 5.0 5.4 5.0 5.0 5.0 5.8 5.7 5.4 5.1
#>  [19] 5.7 5.1 5.4 5.1 5.0 5.1 5.0 5.0 5.0 5.2 5.2 5.0 5.0 5.4 5.2 5.5 5.0 5.0
#>  [37] 5.5 5.0 5.0 5.1 5.0 5.0 5.0 5.0 5.1 5.0 5.1 5.0 5.3 5.0 6.5 6.4 6.5 5.5
#>  [55] 6.5 5.7 6.3 5.0 6.5 5.2 5.0 5.9 6.0 6.1 5.6 6.5 5.6 5.8 6.2 5.6 5.9 6.1
#>  [73] 6.3 6.1 6.4 6.5 6.5 6.5 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.5 6.3 5.6 5.5
#>  [91] 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 6.5 6.3 6.5 6.5 5.0 6.5
#> [109] 6.5 6.5 6.5 6.4 6.5 5.7 5.8 6.4 6.5 6.5 6.5 6.0 6.5 5.6 6.5 6.3 6.5 6.5
#> [127] 6.2 6.1 6.4 6.5 6.5 6.5 6.4 6.3 6.1 6.5 6.3 6.4 6.0 6.5 6.5 6.5 5.8 6.5
#> [145] 6.5 6.5 6.3 6.5 6.2 5.9
winsorize(iris, threshold = 0.2)
#>        Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>   [1,]          5.1         3.4          1.5         0.2       1
#>   [2,]          5.0         3.0          1.5         0.2       1
#>   [3,]          5.0         3.2          1.5         0.2       1
#>   [4,]          5.0         3.1          1.5         0.2       1
#>   [5,]          5.0         3.4          1.5         0.2       1
#>   [6,]          5.4         3.4          1.7         0.4       1
#>   [7,]          5.0         3.4          1.5         0.3       1
#>   [8,]          5.0         3.4          1.5         0.2       1
#>   [9,]          5.0         2.9          1.5         0.2       1
#>  [10,]          5.0         3.1          1.5         0.2       1
#>  [11,]          5.4         3.4          1.5         0.2       1
#>  [12,]          5.0         3.4          1.6         0.2       1
#>  [13,]          5.0         3.0          1.5         0.2       1
#>  [14,]          5.0         3.0          1.5         0.2       1
#>  [15,]          5.8         3.4          1.5         0.2       1
#>  [16,]          5.7         3.4          1.5         0.4       1
#>  [17,]          5.4         3.4          1.5         0.4       1
#>  [18,]          5.1         3.4          1.5         0.3       1
#>  [19,]          5.7         3.4          1.7         0.3       1
#>  [20,]          5.1         3.4          1.5         0.3       1
#>  [21,]          5.4         3.4          1.7         0.2       1
#>  [22,]          5.1         3.4          1.5         0.4       1
#>  [23,]          5.0         3.4          1.5         0.2       1
#>  [24,]          5.1         3.3          1.7         0.5       1
#>  [25,]          5.0         3.4          1.9         0.2       1
#>  [26,]          5.0         3.0          1.6         0.2       1
#>  [27,]          5.0         3.4          1.6         0.4       1
#>  [28,]          5.2         3.4          1.5         0.2       1
#>  [29,]          5.2         3.4          1.5         0.2       1
#>  [30,]          5.0         3.2          1.6         0.2       1
#>  [31,]          5.0         3.1          1.6         0.2       1
#>  [32,]          5.4         3.4          1.5         0.4       1
#>  [33,]          5.2         3.4          1.5         0.2       1
#>  [34,]          5.5         3.4          1.5         0.2       1
#>  [35,]          5.0         3.1          1.5         0.2       1
#>  [36,]          5.0         3.2          1.5         0.2       1
#>  [37,]          5.5         3.4          1.5         0.2       1
#>  [38,]          5.0         3.4          1.5         0.2       1
#>  [39,]          5.0         3.0          1.5         0.2       1
#>  [40,]          5.1         3.4          1.5         0.2       1
#>  [41,]          5.0         3.4          1.5         0.3       1
#>  [42,]          5.0         2.7          1.5         0.3       1
#>  [43,]          5.0         3.2          1.5         0.2       1
#>  [44,]          5.0         3.4          1.6         0.6       1
#>  [45,]          5.1         3.4          1.9         0.4       1
#>  [46,]          5.0         3.0          1.5         0.3       1
#>  [47,]          5.1         3.4          1.6         0.2       1
#>  [48,]          5.0         3.2          1.5         0.2       1
#>  [49,]          5.3         3.4          1.5         0.2       1
#>  [50,]          5.0         3.3          1.5         0.2       1
#>  [51,]          6.5         3.2          4.7         1.4       2
#>  [52,]          6.4         3.2          4.5         1.5       2
#>  [53,]          6.5         3.1          4.9         1.5       2
#>  [54,]          5.5         2.7          4.0         1.3       2
#>  [55,]          6.5         2.8          4.6         1.5       2
#>  [56,]          5.7         2.8          4.5         1.3       2
#>  [57,]          6.3         3.3          4.7         1.6       2
#>  [58,]          5.0         2.7          3.3         1.0       2
#>  [59,]          6.5         2.9          4.6         1.3       2
#>  [60,]          5.2         2.7          3.9         1.4       2
#>  [61,]          5.0         2.7          3.5         1.0       2
#>  [62,]          5.9         3.0          4.2         1.5       2
#>  [63,]          6.0         2.7          4.0         1.0       2
#>  [64,]          6.1         2.9          4.7         1.4       2
#>  [65,]          5.6         2.9          3.6         1.3       2
#>  [66,]          6.5         3.1          4.4         1.4       2
#>  [67,]          5.6         3.0          4.5         1.5       2
#>  [68,]          5.8         2.7          4.1         1.0       2
#>  [69,]          6.2         2.7          4.5         1.5       2
#>  [70,]          5.6         2.7          3.9         1.1       2
#>  [71,]          5.9         3.2          4.8         1.8       2
#>  [72,]          6.1         2.8          4.0         1.3       2
#>  [73,]          6.3         2.7          4.9         1.5       2
#>  [74,]          6.1         2.8          4.7         1.2       2
#>  [75,]          6.4         2.9          4.3         1.3       2
#>  [76,]          6.5         3.0          4.4         1.4       2
#>  [77,]          6.5         2.8          4.8         1.4       2
#>  [78,]          6.5         3.0          5.0         1.7       2
#>  [79,]          6.0         2.9          4.5         1.5       2
#>  [80,]          5.7         2.7          3.5         1.0       2
#>  [81,]          5.5         2.7          3.8         1.1       2
#>  [82,]          5.5         2.7          3.7         1.0       2
#>  [83,]          5.8         2.7          3.9         1.2       2
#>  [84,]          6.0         2.7          5.1         1.6       2
#>  [85,]          5.4         3.0          4.5         1.5       2
#>  [86,]          6.0         3.4          4.5         1.6       2
#>  [87,]          6.5         3.1          4.7         1.5       2
#>  [88,]          6.3         2.7          4.4         1.3       2
#>  [89,]          5.6         3.0          4.1         1.3       2
#>  [90,]          5.5         2.7          4.0         1.3       2
#>  [91,]          5.5         2.7          4.4         1.2       2
#>  [92,]          6.1         3.0          4.6         1.4       2
#>  [93,]          5.8         2.7          4.0         1.2       2
#>  [94,]          5.0         2.7          3.3         1.0       2
#>  [95,]          5.6         2.7          4.2         1.3       2
#>  [96,]          5.7         3.0          4.2         1.2       2
#>  [97,]          5.7         2.9          4.2         1.3       2
#>  [98,]          6.2         2.9          4.3         1.3       2
#>  [99,]          5.1         2.7          3.0         1.1       2
#> [100,]          5.7         2.8          4.1         1.3       2
#> [101,]          6.3         3.3          5.3         1.9       3
#> [102,]          5.8         2.7          5.1         1.9       3
#> [103,]          6.5         3.0          5.3         1.9       3
#> [104,]          6.3         2.9          5.3         1.8       3
#> [105,]          6.5         3.0          5.3         1.9       3
#> [106,]          6.5         3.0          5.3         1.9       3
#> [107,]          5.0         2.7          4.5         1.7       3
#> [108,]          6.5         2.9          5.3         1.8       3
#> [109,]          6.5         2.7          5.3         1.8       3
#> [110,]          6.5         3.4          5.3         1.9       3
#> [111,]          6.5         3.2          5.1         1.9       3
#> [112,]          6.4         2.7          5.3         1.9       3
#> [113,]          6.5         3.0          5.3         1.9       3
#> [114,]          5.7         2.7          5.0         1.9       3
#> [115,]          5.8         2.8          5.1         1.9       3
#> [116,]          6.4         3.2          5.3         1.9       3
#> [117,]          6.5         3.0          5.3         1.8       3
#> [118,]          6.5         3.4          5.3         1.9       3
#> [119,]          6.5         2.7          5.3         1.9       3
#> [120,]          6.0         2.7          5.0         1.5       3
#> [121,]          6.5         3.2          5.3         1.9       3
#> [122,]          5.6         2.8          4.9         1.9       3
#> [123,]          6.5         2.8          5.3         1.9       3
#> [124,]          6.3         2.7          4.9         1.8       3
#> [125,]          6.5         3.3          5.3         1.9       3
#> [126,]          6.5         3.2          5.3         1.8       3
#> [127,]          6.2         2.8          4.8         1.8       3
#> [128,]          6.1         3.0          4.9         1.8       3
#> [129,]          6.4         2.8          5.3         1.9       3
#> [130,]          6.5         3.0          5.3         1.6       3
#> [131,]          6.5         2.8          5.3         1.9       3
#> [132,]          6.5         3.4          5.3         1.9       3
#> [133,]          6.4         2.8          5.3         1.9       3
#> [134,]          6.3         2.8          5.1         1.5       3
#> [135,]          6.1         2.7          5.3         1.4       3
#> [136,]          6.5         3.0          5.3         1.9       3
#> [137,]          6.3         3.4          5.3         1.9       3
#> [138,]          6.4         3.1          5.3         1.8       3
#> [139,]          6.0         3.0          4.8         1.8       3
#> [140,]          6.5         3.1          5.3         1.9       3
#> [141,]          6.5         3.1          5.3         1.9       3
#> [142,]          6.5         3.1          5.1         1.9       3
#> [143,]          5.8         2.7          5.1         1.9       3
#> [144,]          6.5         3.2          5.3         1.9       3
#> [145,]          6.5         3.3          5.3         1.9       3
#> [146,]          6.5         3.0          5.2         1.9       3
#> [147,]          6.3         2.7          5.0         1.9       3
#> [148,]          6.5         3.0          5.2         1.9       3
#> [149,]          6.2         3.4          5.3         1.9       3
#> [150,]          5.9         3.0          5.1         1.8       3