简体   繁体   中英

Elegant R function: mixed case separated by periods to underscore separated lower case and/or camel case

I often get datasets from collaborators that have non-consistent naming of variables/columns in the dataset. One of my first tasks is to rename them, and I want a solution completely within R to do so.

as.Given <- c("ICUDays","SexCode","MAX_of_MLD","Age.Group")

underscore_lowercase <- c("icu_days", "sex_code", "max_of_mld","age_group")

camelCase <- c("icuDays", "sexCode", "maxOfMld", "ageGroup")

Given the different opinions about naming conventions and in the spirit of what was proposed in Python , what ways are there to go from as.Given to underscore_lowercase and/or camelCase in a user-specified way in R?

Edit: Also found this related post in R / regex , especially the answer of @rengis.

Try this. These at least work on the examples given:

toUnderscore <- function(x) {
  x2 <- gsub("([A-Za-z])([A-Z])([a-z])", "\\1_\\2\\3", x)
  x3 <- gsub(".", "_", x2, fixed = TRUE)
  x4 <- gsub("([a-z])([A-Z])", "\\1_\\2", x3)
  x5 <- tolower(x4)
  x5
}

underscore2camel <- function(x) {
  gsub("_(.)", "\\U\\1", x, perl = TRUE)
}

#######################################################
# test
#######################################################

u <- toUnderscore(as.Given)
u
## [1] "icu_days"   "sex_code"   "max_of_mld" "age_group" 

underscore2camel(u)
## [1] "icuDays"  "sexCode"  "maxOfMld" "ageGroup"

To get the second underscore_lowercase ( g ) and camelCase ( x ) strings,

> as.Given <- c("ICUDays","SexCode","MAX_of_MLD","Age.Group")
> r <- gsub("[^\\w]", "", as.Given, perl=T)
> f <- gsub("^.*?_.*$(*SKIP)(*F)|(?:[^A-Z]+|[A-Z_]+?)\\K([A-Z])(?=[A-Z_]+$|[a-z_]+$)", "_\\1", r,perl=T)
> g <- tolower(f)
> g
[1] "icu_days"   "sex_code"   "max_of_mld" "age_group"
> x <- gsub("_([a-z])", "\\U\\1", g,perl=T)
> x
[1] "icuDays"  "sexCode"  "maxOfMld" "ageGroup"

UPDATE

> as.Given = c("CRMLevel1Code", "MAX_of_RhD", "MAX_Of_MCa", "MAX_of_NCCexclusion","ICUDays","SexCode","MAX_of_MLD","Age.Group","admitRom")
> r <- gsub("[^\\w]", "", as.Given, perl=T)
> f <- gsub("(?:[^A-Z]|^)[A-Z][A-Z][A-Z]\\K(?=[a-zA-Z])|(?=\\d)|^[A-Z][a-z]+\\K(?=[A-Z][a-z]+$)|(?<=\\d)(?=[A-Za-z])|^[a-z]+\\K(?=[A-Z][a-z]+$)", "_", r, perl=T)
> underscore_lowercase <- tolower(f)
> underscore_lowercase
[1] "crm_level_1_code"     "max_of_rhd"           "max_of_mca"          
[4] "max_of_ncc_exclusion" "icu_days"             "sex_code"            
[7] "max_of_mld"           "age_group"            "admit_rom"           
> camelCase <- gsub("_([a-z]|\d)", "\\U\\1", underscore_lowercase, perl=T)
Error: '\d' is an unrecognized escape in character string starting ""_([a-z]|\d"
> camelCase <- gsub("_([a-z]|\\d)", "\\U\\1", underscore_lowercase, perl=T)
> camelCase
[1] "crmLevel1Code"     "maxOfRhd"          "maxOfMca"         
[4] "maxOfNccExclusion" "icuDays"           "sexCode"          
[7] "maxOfMld"          "ageGroup"          "admitRom" 

Based off your as.Given vector and adding admitROM to the list, this will do the trick.

as.Given <- c('ICUDays', 'SexCode', 'MAX_of_MLD', 'Age.Group', 'admitROM')
invertd <- gsub('(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|\\.', '_', as.Given, perl=T)
toscore <- tolower(invertd)
## [1] "icu_days"   "sex_code"   "max_of_mld" "age_group"  "admit_rom" 
tocamel <- gsub("_([a-z])", "\\U\\1", toscore, perl=T)
## [1] "icuDays"  "sexCode"  "maxOfMld" "ageGroup" "admitRom"

This should do the trick:

install.packages("snakecase")
library(snakecase)

to_snake_case(as.Given)
#> [1] "icu_days"   "sex_code"   "max_of_mld" "age_group" 

to_lower_camel_case(as.Given)
#> [1] "icuDays"  "sexCode"  "maxOfMld" "ageGroup"

Githublink to snakecase package: https://github.com/Tazinho/snakecase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM