winsorizor {JWileymisc} | R Documentation |
Winsorize at specified percentiles
Description
Simple function winsorizes data at the specified percentile.
Usage
winsorizor(d, percentile, values, na.rm = TRUE)
Arguments
d |
A vector, matrix, data frame, or data table to be winsorized |
percentile |
The percentile bounded by [0, 1] to winsorize data at. If a data frame or matrix is provided for the data, this should have the same length as the number of columns, or it will be repeated for all. |
values |
If values are specified, use these instead of calculating by percentiles. Should be a data frame with columns named “low”, and “high”. If a data frame or matrix is provided for the data, there should be as many rows for values to winsorize at as there are columns in the data. |
na.rm |
A logical whether to remove NAs. |
Value
winsorized data. Attributes are included to list the exact values (for each variable, if a data frame or matrix) used to winsorize at the lower and upper ends.
Examples
dev.new(width = 10, height = 5)
par(mfrow = c(1, 2))
hist(as.vector(eurodist), main = "Eurodist")
hist(winsorizor(as.vector(eurodist), .05), main = "Eurodist with lower and upper\n5% winsorized")
library(data.table)
dat <- data.table(x = 1:5)
dat[, y := scale(1:5)]
winsorizor(dat$y, .01)
## make a copy of the data table
winsorizor(dat, .01)
winsorizor(mtcars, .01)
winsorizor(matrix(1:9, 3), .01)
rm(dat) # clean up