R/rename_std.R

Defines functions rename_std

Documented in rename_std

#' Rename df columns
#' @description rename all variables within a data frame (standardized syntax).
#'
#' @param .data An object of class data.frame
#'
#'@references see https://www.r-bloggers.com/clean-consistent-column-names/
#'
#' @export
rename_std <- function(.data) {
  # inspired by
  # https://www.r-bloggers.com/clean-consistent-column-names/
  n <- if (is.data.frame(.data)) colnames(.data) else .data

  n <- gsub("[^a-zA-Z0-9_]+", "_", n)
  n <- gsub("([A-Z][a-z])", "_\\1", n)
  n <- toupper(trimws(n))

  n <- gsub("(^_+|_+$)", "", n)

  n <- gsub("_+", "_", n)
}
sachserf/sf documentation built on June 8, 2024, 4:23 p.m.