R/decay_binary.R

Defines functions decay_binary

Documented in decay_binary

#' Binary (a.k.a. step) decay function
#'
#' Returns a binary weighting function (frequently used to calculate cumulative
#' opportunities measures) to be used inside accessibility calculating
#' functions.
#' @template description_generic_cost
#'
#' @param cutoff A `numeric` vector. The numbers indicating the travel cost
#'   cutoffs.
#'
#' @template return_decay_function
#'
#' @family decay functions
#'
#' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
#' weighting_function <- decay_binary(cutoff = 30)
#'
#' weighting_function(c(20, 35))
#'
#' weighting_function <- decay_binary(cutoff = c(30, 45))
#'
#' weighting_function(c(20, 35))
#'
#' @export
decay_binary <- function(cutoff) {
  checkmate::assert_numeric(
    cutoff,
    lower = 0,
    finite = TRUE,
    any.missing = FALSE,
    min.len = 1,
    unique = TRUE
  )

  weighting_function <- function(travel_cost) {
    weights_list <- lapply(
      cutoff,
      function(x) {
        weights <- as.integer(travel_cost <= x)
        weights
      }
    )
    names(weights_list) <- cutoff

    return(weights_list)
  }

  return(weighting_function)
}

Try the accessibility package in your browser

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

accessibility documentation built on May 29, 2024, 7:29 a.m.