fix_tbl_names: Clean column names from a data.frame or chr vector

View source: R/fix_tbl_names.R

fix_tbl_namesR Documentation

Clean column names from a data.frame or chr vector

Description

Remove non-alphanum, replace whitespace with uscore, optionally lower, dedupe

Usage

fix_tbl_names(x, lower = TRUE, uniquify = TRUE)

Arguments

x

A character vector or a data.frame. If the latter, will extract names

lower

Lowercase the output? Defaults to TRUE

uniquify

should duplicate names be made unique? Defaults to TRUE

Details

This is a simple utility that replaces any non-alphanumeric characters in x with an underscore, ensuring that runs of underscores > 1 are truncated to length 1, stripping any trailing underscores, and optionally lowercasing the output (default TRUE).

If uniquify=TRUE (default), any duplicated values after the above are handled via make.unique()

Value

A character vector of cleaned-up names

Examples

x <- c("This is (a dirty) name  with ", "A", "a")
fix_tbl_names(x)
fix_tbl_names(x, lower = FALSE)

# by default, duplicate names are made unique
xdf <- data.frame(1:10, LETTERS[1:10], letters[1:10])
names(xdf) <- x
fix_tbl_names(xdf) # default
fix_tbl_names(xdf, uniquify = FALSE) # allow duplicate names post-lowercase

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.