#' 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
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.