[英]Elegant R function: mixed case separated by periods to underscore separated lower case and/or camel case
我經常從協作者處獲得數據集,這些數據集在數據集中具有不一致的變量/列命名。 我的首要任務之一是重命名它們,我想在R內部完全解決這個問題。
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")
鑒於對命名約定的不同看法以及Python中提出的內容的精神,有什么方法可以在R中以用戶指定的方式從as.Given
轉到underscore_lowercase
和/或camelCase
?
編輯: 還在R / regex中找到了這個相關的帖子 ,特別是@rengis的答案。
試試這個。 這些至少可以用於給出的例子:
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"
要獲得第二個underscore_lowercase
( g )和camelCase
( x )字符串,
> 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"
基於你的as.Given
向量並將admitROM
添加到列表中,這將admitROM
。
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"
這應該做的伎倆:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.