R/util_observations_in_subgroups.R

Defines functions util_observations_in_subgroups

Documented in util_observations_in_subgroups

#' Utility function observations in subgroups
#'
#' This function uses `!is.na` to count the number of non-missing observations in subgroups of
#' the data (list) and in a set of user defined response variables. In some applications it is
#' required that the number of observations per e.g. factor level is higher than a user-defined
#' minimum number.
#'
#' @param x data frame
#' @param rvs variable names
#'
#' @return matrix of flags
#'
#' @seealso [prep_min_obs_level]
#' @seealso [util_check_group_levels]
#'
#' @family robustness_functions
#' @concept data_management
#' @keywords internal
util_observations_in_subgroups <- function(x, rvs) {
  if (dim(x)[1] == 0) {
    x[1, ] <- t(rep(NA, length(x)))
  }

  y <- !apply(x[, rvs, drop = FALSE], 2, is.na)


  if (!is.matrix(y)) {
    y <- as.matrix(t(y))
  }

  colnames(y) <- paste0("OBSERVATIONS_OUT_", rvs)
  x <- cbind(x, y)
}

Try the dataquieR package in your browser

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

dataquieR documentation built on May 29, 2024, 7:18 a.m.