R/setOrderList.R

Defines functions setOrderList

Documented in setOrderList

#' Reorder List Elements by Name
#'
#' Moves specified elements to the front and/or back of a list while 
#' preserving the relative order of all other elements.
#'
#' @param l A named list.
#' @param first A character vector of names to move to the front.
#' @param last A character vector of names to move to the back.
#'
#' @return A list with reordered elements. Names that do not exist 
#' in \code{l} are silently ignored.
#'
#' @export
#'
#' @examples
#' my_list <- list(c = 3, a = 1, d = 4, b = 2)
#' setOrderList(my_list, first = c("a", "b"), last = "c")
setOrderList <- function(l, first = NULL, last = NULL) {
  if (any(first %in% last)) {
    stop("The same name cannot appear in both 'first' and 'last' arguments.")
  }

  nms <- names(l)
  
  prio_first <- intersect(first, nms)
  prio_last <- intersect(last, nms)
  rest <- setdiff(nms, union(first, last))
  
  l[c(prio_first, rest, prio_last)]
}

Try the NMdata package in your browser

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

NMdata documentation built on June 17, 2026, 9:06 a.m.