snake_case: Convert variable names to snake_case

View source: R/snake-case.R

snake_caseR Documentation

Convert variable names to snake_case

Description

This function attempts to convert variables to snake_case, even if it's already in snake_case. The important regex lines were posted by Stack Overflow user epost in "Elegant Python function to convert CamelCase to snake_case?".

Usage

snake_case(x)

Arguments

x

A vector of names to convert.

Value

A vector of converted names.

Note

This series of regexes has an advantages over the current implementations of lettercase::str_snake_case() and snakecase::to_snake_case(). The former converts "PatientDOB" to "patientdob" and the latter converts "patient.dob" to "patient_._dob". I'll keep an eye on these packages (i.e., lettercase #1 for 'camelCase' and snakecase #101). I'd prefer to use one of them, instead of maintaining the functions.

Author(s)

Will Beasley

Examples

snake_case(colnames(datasets::OrchardSprays))
snake_case(colnames(datasets::iris))
snake_case(c("PatientID", "PatientDOB", "DOB", "name.last", "name.first"))

OuhscBbmc/OuhscMunge documentation built on Dec. 5, 2024, 4:34 a.m.