R/fix_factor_levels.R

Defines functions fix_factor_levels.Matrix fix_factor_levels.data.table fix_factor_levels

fix_factor_levels = function(data, levels, ...) {
  UseMethod("fix_factor_levels")
}

#' @export
fix_factor_levels.data.table = function(data, levels, ...) { # nolint
  levels = levels[intersect(names(levels), names(data))]
  iwalk(levels, function(lvls, id, data) {
    x = data[[id]]
    if (!identical(levels(x), lvls)) {
      set(data, j = id, value = factor(x, levels = lvls, ordered = is.ordered(x)))
    }
  }, data = data)
  data[]
}

#' @export
fix_factor_levels.Matrix = function(data, levels, ...) { # nolint
  levels = levels[intersect(names(levels), names(data))]
  if (length(levels)) {
    stop("Factor handling in Matrix data is not supported")
  }
  data
}

Try the mlr3 package in your browser

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

mlr3 documentation built on Nov. 17, 2023, 5:07 p.m.