R/unfactor.R

#' Convert factors to the type they hide
#'
#' @param x An object
#'
#' @return A modified object that is not a factor, and doesn't contain factor elements
#' @export
#'
#' @examples
# unfactor(factor(c(1,2,3)))       # [1] 1 2 3
# unfactor(factor(c("a","b","c"))) # [1] "a" "b" "c"
# iris2 <- iris
# iris2$Sepal.Width <- factor(iris2$Sepal.Width)
# str(iris2)
# str(unfactor(iris))
#'
unfactor <- function(x){
  UseMethod("unfactor")
}

unfactor.default <- function(x){
  x
}

unfactor.factor <- function(x){
  if (hides_num(x)) {
    as.numeric(x)
  } else {
    as.character(x)
  }
}

unfactor.list <- function(x){
    x[] <- lapply(x,unfactor)
    x
}

unfactor.environment <- function(x){
  unfactor.list(x)
}

unfactor.data.frame <- function(x){
  unfactor.list(x)
}
moodymudskipper/hidden documentation built on May 20, 2019, 9:59 a.m.