R/utils.R

Defines functions min_diff normalise is_constant expand.grid2 is_identity max_na min_na

## helper functions ------------------
min_na <- function(...) {
  min(..., na.rm = TRUE)
}

max_na <- function(...) {
  max(..., na.rm = TRUE)
}

is_identity <- function(x) { # x is quosure
  if (is_quosure(x)) {
    return(quo_get_expr(x) == 1)
  }
  "1" %in% as.character(map(x, quo_get_expr))
}

expand.grid2 <- function(...) {
  expand.grid(..., KEEP.OUT.ATTRS = FALSE, stringsAsFactors = FALSE)
}

is_constant <- function(x) {
  diff(range(x, na.rm = TRUE)) < .Machine$double.eps ^ 0.5
}

# Normalise the numerics to range from 0 to 1
normalise <- function(x, xmin = NULL, xmax = NULL) {
  if (is_constant(x)) return(x)
  if (is.null(xmin)) xmin <- min_na(x)
  if (is.null(xmax)) xmax <- max_na(x)
  (x - xmin) / (xmax - xmin)
}

min_diff <- function(x) {
  min(abs(diff(x, na.rm = TRUE)))
}

Try the sugrrants package in your browser

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

sugrrants documentation built on Jan. 13, 2021, 5:06 a.m.