#' Tukey coefficient
#'
#' Calculates a Tukey's coefficient
#'
#' @param x A vector of numbers
#'
#' @importFrom stats quantile
#' @export
#' @examples
#' tukey_coef(0:20)
#' tukey_coef(stats::rchisq(100, 2))
tukey_coef <- function(x) {
stopifnot(is.numeric(x))
qq <- quantile(x, c(0.25, 0.75), names = FALSE, na.rm = TRUE)
iqr <- diff(qq)
vapply(x,
function(x) {
if(is.na(x)) {
NA_real_
} else if(x < qq[1]) {
(x - qq[1]) / iqr
} else if(x > qq[2]) {
(x - qq[2]) / iqr
} else {
0.0
}
}, double(1),
USE.NAMES = FALSE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.