R/safe_max.R

Defines functions safe_max

Documented in safe_max

#' Safely calculating the max.
#'
#' @param x A number.
#' @param na.rm Boolean.
#' @returns A number.
#' @export
safe_max <- function(x, na.rm = FALSE) {
  if (na.rm) {
    x <- x[!is.na(x)]
  }
  if (length(x) == 0) {
    return(NA)  # or choose another value you prefer to represent the empty set case
  } else {
    return(max(x))
  }
}

Try the tidyedgar package in your browser

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

tidyedgar documentation built on May 29, 2024, 6:45 a.m.