R/utils.guess.factor.R

Defines functions guess.factor

#-----------------------------------------------------------------------
# There are many functions in which guessing if something is a factor is useful
# therefore this should act as a bridge.
#' @export
guess.factor <- function(x,..., DEBUG=FALSE, max.factor.cat= 10){
  if(DEBUG) cat("\n[.guess.factor] ->",class(x),"\n")

  args <- list(...)

  if(!is.data.frame(x) & !is.factor(x)){
    total.cat <- length(unique(x))
    length.limit <- (length(x)*.1)

    if(DEBUG) cat("\n[.guess.factor] Guess factor ->",
                  "\n max.factor =", max.factor.cat,
                  "\n total.cat =", total.cat,
                  "\n length.limit =", length.limit,
                  "\n")
    if((total.cat <= max.factor.cat) & (total.cat < length.limit)) {
      #.... if we have a numeric variable with less than max.factor.cat different values
      #     AND there are less than 10% of the length of the vector in different values
      #     then we can guess that it's a factor
      x <- factor(x)
      if(DEBUG) cat("\n[.guess.factor] -> Factor guess: IS FACTOR =>",class(x),"\n")
    }
  }

  return(x)
}
feranpre/feR documentation built on Nov. 22, 2022, 2:29 a.m.