R/commonYears.R

Defines functions commonYears

Documented in commonYears

#' commonYears
#'
#' Given (a list of) magclass objects, return the years (getYears) they have in common,
#' ignoring magclass objects with 0 or 1 years.
#'
#' @param ... One or more magclass objects, or alternatively a list of magclass objects.
#' @param asInteger Passed on to getYears. If TRUE the output format is 1995 otherwise "y1995".
#' @return The years (as returned by getYears) the input objects have in common.
#'
#' @author Pascal Sauer
#' @family TemporalOperations
#' @export
commonYears <- function(..., asInteger = FALSE) {
  x <- list(...)
  if (length(x) == 1 && is.list(x[[1]])) {
    x <- x[[1]]
  }
  stopifnot(vapply(x, is.magpie, logical(1)))
  x <- Filter(function(i) nyears(i) > 1, x)
  return(Reduce(intersect, lapply(x, getYears, as.integer = asInteger)))
}

Try the magclass package in your browser

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

magclass documentation built on April 17, 2026, 5:07 p.m.