#' Replace outliers in the variable with one of summary statistic for that variable or for a grouped by category summary statistic
#'
#' @param data Data frame from which to calculate the replacement number.
#'
#' @param group_var Variable for grouping.
#'
#' @param target_var Variable in which outliers are to be replaced.
#'
#' @param group Group or category for which to calculate the replacement number.
#'
#' @param replacement_stat Which summary statistic to use. "Median" by default.
#' @return Single number, summary statistic.
#' @examples
#' group_median <- replace_outliers(ad_metrics, 96)
#'
#' @export replace_outliers
#' @import tidyverse
replace_outliers <- function(data, group_var, target_var, group, replacement_stat = "median") {
group_var <- enquo(group_var)
target_var <- enquo(target_var)
if (replacement_stat == "median") {
tmp_df <- data %>%
group_by(!!group_var) %>%
summarize(var_median = median(!!target_var, na.rm = TRUE)) %>%
ungroup() %>%
filter(!!group_var == group)
return(tmp_df$var_median)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.