加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 优雅R功能:混合大小写,以句点分隔,以分隔小写和/

发布时间:2020-12-14 06:34:31 所属栏目:百科 来源:网络整理
导读:我经常从数据集中的变量/列的不一致命名的协作者获取数据集。我的第一个任务之一是重命名它们,我想在R中完全解决这个问题。 as.Given - c("ICUDays","SexCode","MAX_of_MLD","Age.Group")underscore_lowercase - c("icu_days","sex_code","max_of_mld","age
我经常从数据集中的变量/列的不一致命名的协作者获取数据集。我的第一个任务之一是重命名它们,我想在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")

鉴于different opinions about naming conventions和what was proposed in Python的精神,在R中以用户指定的方式从as.Given到underscore_lowercase和/或camelCase有什么方法?

编辑:Also found this related post in R / regex,特别是@rengis的答案。

尝试这个。这些至少在以下示例上工作:
toUnderscore <- function(x) {
  x2 <- gsub("([A-Za-z])([A-Z])([a-z])","1_23",x)
  x3 <- gsub(".","_",x2,fixed = TRUE)
  x4 <- gsub("([a-z])([A-Z])","1_2",x3)
  x5 <- tolower(x4)
  x5
}

underscore2camel <- function(x) {
  gsub("_(.)","U1",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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读