Vectorized winsorization for use with dplyr. For group-wise winsorization, use dplyr::group_by() before mutate().
Usage
winsorize_vec(
x,
method = c("zscore", "iqr", "percentile"),
threshold = NULL,
na.rm = TRUE
)Examples
if (FALSE) { # \dontrun{
library(dplyr)
# Winsorize at 3 SD
mtcars %>% mutate(hp_w = winsorize_vec(hp, method = "zscore", threshold = 3))
# Winsorize at 5th/95th percentile
mtcars %>% mutate(hp_w = winsorize_vec(hp, method = "percentile", threshold = 0.05))
# Group-wise winsorization
mtcars %>% group_by(cyl) %>% mutate(hp_w = winsorize_vec(hp))
} # }