R/helper_subsetting.R

Defines functions subset_array extract_last_margin

extract_last_margin <- function(arr) {
  idx <- c(rep(list(1), length(dim(arr)) - 1), list(seq_len(rev(dim(arr))[1])))
  do.call(`[`, c(list(arr), idx))
}

subset_array <- function(array, margin, groups) {
  index_list <- lapply(dim(array), \(x) 1:x)
  index_list[[margin]] <- groups
  do.call(`[`, c(list(array), index_list, drop = FALSE))
}

Try the RSTr package in your browser

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

RSTr documentation built on Jan. 31, 2026, 9:07 a.m.