R/utilities.R

Defines functions lapplyFUNpairwise check_for_two_levels

Documented in check_for_two_levels lapplyFUNpairwise

#' Checks whether a vector has more than two unique values
#'
#' @param x a vector
#' @keywords internal

check_for_two_levels <- function(x) {
  xunique <- sort(unique(x))
  if (length(xunique) == 2 && all(xunique != c(0, 1))) {
    warning(sprintf("x has two levels, but they are not code as 0/1. SMD is being computed based on the values %s.", paste(xunique, collapse = ",")))
  }
}

#' Apply a function pairwise along a list
#'
#' Applies a function \code{f(ref, y)} where \code{y} is an element of a list and
#'  \code{ref} is a reference element.
#'
#' @param x a list
#' @param f the function to apply
#' @param ref the index of the reference element
#' @keywords internal

lapplyFUNpairwise <- function(x, f, ref) {
  if (sum(is.na(x)) >= 1) stop("x contains NA value")
  lapply(x[-ref], FUN = function(y) {
    f(x[[ref]], y)
  })
}

Try the smd package in your browser

Any scripts or data that you put into this service are public.

smd documentation built on May 29, 2024, 3:10 a.m.