R/step_find_interval.R

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Divide a Term into Intervals and do Dummy Encoding ---------------------------
#
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
StepFindInterval <- R6Class(
  classname = "step_find_interval",
  inherit = Step,
  public = list(

    # step specific variables
    vec = NULL,
    n_vec = NULL,

    initialize = function(terms,
                          vec,
                          role = "predictor",
                          ...) {
      # get function parameters to pass to parent
      terms <- substitute(terms)
      env_list <- get_function_arguments()
      env_list$step_name <- "step_find_interval"
      env_list$type <- "add"
      super$initialize(
        terms = terms,
        env_list[names(env_list) != "terms"],
        ...
      )

      # step specific values
      self$vec <- vec[order(vec)] # slightly faster than sort
      self$n_vec <- length(vec)

      invisible(self)
    },
    bake = function(s) {


      self$new_columns <- c()

      dum <- list()
      for (i in seq_along(self$columns)) {

        column_name <- self$columns[i]

        dum[[i]] <- to_dummy_list(s[["result"]][[column_name]], self$vec)

        nn <- name_columns(
          self$prefix,
          column_name,
          length(dum[[i]])
        )

        names(dum[[i]]) <- nn
        self$new_columns <- c(self$new_columns, nn)
      }

      self$result <- unlist(dum, recursive = FALSE)
      # self$result
      return(NULL)

    }
  )
)
jkennel/hydrorecipes documentation built on Dec. 24, 2024, 5:38 p.m.