R/trans_hierarchy_cut.R

Defines functions transform.hierarchy_cut hierarchy_cut

Documented in hierarchy_cut

#'@title Hierarchy mapping by cut
#'@description Create a categorical hierarchy from a numeric attribute using cut points.
#'@param attribute numeric attribute to discretize
#'@param breaks numeric breakpoints for `cut`
#'@param labels optional labels for the cut intervals
#'@param new_attribute name of the new attribute (default: "attribute.Level")
#'@return returns an object of class `hierarchy_cut`
#'@examples
#'data(iris)
#'hc <- hierarchy_cut(
#'  "Sepal.Length",
#'  breaks = c(-Inf, 5.5, 6.5, Inf),
#'  labels = c("baixo", "medio", "alto")
#')
#'iris_h <- transform(hc, iris)
#'table(iris_h$Sepal.Length.Level)
#'@export
hierarchy_cut <- function(attribute, breaks, labels = NULL, new_attribute = NULL) {
  obj <- dal_transform()
  obj$attribute <- attribute
  obj$breaks <- breaks
  obj$labels <- labels
  obj$new_attribute <- new_attribute
  class(obj) <- append("hierarchy_cut", class(obj))
  return(obj)
}

#'@exportS3Method transform hierarchy_cut
transform.hierarchy_cut <- function(obj, data, ...) {
  data <- adjust_data.frame(data)
  attribute <- obj$attribute
  if (is.null(attribute) || !attribute %in% names(data)) {
    stop("hierarchy_cut: 'attribute' must be a valid column name in data.")
  }
  new_attribute <- obj$new_attribute
  if (is.null(new_attribute)) {
    new_attribute <- paste0(attribute, ".Level")
  }

  data[[new_attribute]] <- cut(
    data[[attribute]],
    breaks = obj$breaks,
    labels = obj$labels,
    include.lowest = TRUE
  )
  return(data)
}

Try the daltoolbox package in your browser

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

daltoolbox documentation built on Feb. 10, 2026, 9:06 a.m.