R/detect_fct.R

Defines functions detect_fct

Documented in detect_fct

#' Detect whether a factor is truly factor or character
#'
#' If the number of unique values equal to the length of input, we consider the input as factor; otherwise,
#' we consider it as character
#'
#' @param x factor
#'
#' @return boolean; \code{TRUE} if input should be considered as factor,
#'    \code{FALSE} if input should be considered as character
#' @export
#'
#' @examples
#' a <- factor(c("high", "high", "low"))
#' b <- factor(c("high", "low", "medium"))
#'
#' detect_fct(a)
#'
#' detect_fct(b)
#'
detect_fct <- function(x) {
  if (is.factor(x)) {
    length(x) != length(unique(x))
  } else {
    stop("Input value should belong to class factor, you privided a class of ", class(x)[1])
  }
}
qiaoyuet/foofactors documentation built on May 29, 2019, 12:06 p.m.