R/dth_clean_names.R

Defines functions dth_clean_names

Documented in dth_clean_names

#' Return R-compatible column names for a `data.table` 
#'
#' @param .data 
#' @param unique 
#'
#' @return
#' @export
#'
#' @examples
dth_clean_names <- function(.data, unique = FALSE) {
  ## credit: https://drdoane.com/clean-consistent-column-names/
  n <- if (is.data.frame(.data)) colnames(.data) else .data
  
  n <- gsub("%+", "_pct_", n)
  n <- gsub("\\$+", "_dollars_", n)
  n <- gsub("\\++", "_plus_", n)
  n <- gsub("-+", "_minus_", n)
  n <- gsub("\\*+", "_star_", n)
  n <- gsub("#+", "_cnt_", n)
  n <- gsub("&+", "_and_", n)
  n <- gsub("@+", "_at_", n)
  
  n <- gsub("[^a-zA-Z0-9_]+", "_", n)
  n <- gsub("([A-Z][a-z])", "_\\1", n)
  n <- tolower(trimws(n))
  
  n <- gsub("(^_+|_+$)", "", n)
  
  n <- gsub("_+", "_", n)
  
  if (unique) n <- make.unique(n, sep = "_")
  
  if (is.data.frame(.data)) {
    colnames(.data) <- n
    .data
  } else {
    n
  }
}
dbrookeUAB/dth documentation built on Oct. 6, 2022, 10:52 p.m.