R/utils.R

# functions taken from 'clubSandwich' package
matrix_split <- function (x, fac, dim) {
  if (is.vector(x)) {
    if (dim != "both")
      cli::cli_abort(
        paste0("Object must be a matrix in order to subset by ",
                  dim, ".")
      )
    x_list <- split(x, fac)
    lapply(x_list, function(x) diag(x, nrow = length(x)))
  }
  else {
    lapply(levels(fac), sub_f(x, fac, dim))
  }
}

sub_f <- function (x, fac, dim){
  function(f) switch(dim,
                     row = x[fac == f, , drop = FALSE],
                     col = x[, fac == f, drop = FALSE],
                     both = x[fac == f,
                              fac == f, drop = FALSE])
}

Try the summclust package in your browser

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

summclust documentation built on Aug. 10, 2023, 9:07 a.m.