R/Tdiff.R

Defines functions Tdiff

Documented in Tdiff

#' Two-group absolute-difference statistics.
#'
#' @param value Value variable of length `nobs`.
#' @param group Grouping variable of length `nobs`.  Must contain exactly two distinct values.
#' @return A vector of length two, containing the absolute difference between group means and group medians, respectively.
#' @export
Tdiff <- function(value, group) {
  grnm <- sort(unique(group))
  ngr <- length(grnm)
  if(ngr != 2) stop("group must contain exactly two distinct values.")
  x <- matrix(NA, ngr, 2)
  colnames(x) <- c("mean", "median")
  for(ii in 1:ngr) {
    v <- value[group == grnm[ii]]
    x[ii,] <- c(mean(v), median(v))
  }
  abs(x[2,] - x[1,])
}
mlysy/aq2020 documentation built on April 14, 2022, 12:43 a.m.