R/ifelse.R

#' @importFrom methods new
#' @exportMethod ifelse
setMethod("ifelse",
  signature(test = "magpie"),
  function(test, yes, no) {
    if (!all(suppressWarnings(as.integer(test)) %in% c(0, 1))) {
      stop("'test' must only contain booleans!")
    }
    yes <- test * yes
    no <- (1 - test) * no
    yes[!test] <- 0
    no[test] <- 0
    return(yes + no)
  }
)

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.