R/drop_unused_factor_levels.R

Defines functions drop_unused_factor_levels

Documented in drop_unused_factor_levels

#' Drop unused factor levels for dataframe
#'
#' Drop unused factor levels from all factor variables in the dataframe.
#'
#' @param df dataframe
#' @param var_selection Character vector of selected variables to be set missing. Default \code{var_selection = c("all")} means that
#'                      all variables are selected
#'
#' @return dataframe
#' @export


drop_unused_factor_levels <- function(df, var_selection = c("_all")) {

  #drop factor levels
  if(var_selection == "_all"){
  df %>%
    dplyr::mutate_if(is.factor, as.factor)
  } else {
    df %>%
      dplyr::mutate_at(var_selection, as.factor)
  }

}
marianschmidt/msAutolabelR documentation built on April 17, 2022, 7:42 a.m.