如何在R中将not.camel.case转换为CamelCase

Tom*_*rot 23 camelcasing r

在R中,我想转换

t1 <- c('this.text', 'next.text')
"this.text" "next.text"
Run Code Online (Sandbox Code Playgroud)

'ThisText' 'NextText'
Run Code Online (Sandbox Code Playgroud)

我试过了

gsub('\\..', '', t1)
Run Code Online (Sandbox Code Playgroud)

但这给了我

"thisext" "nextext"
Run Code Online (Sandbox Code Playgroud)

因为在这段时间之后它不会取代这封信.

可能真的很容易,但我无法解决.

joh*_*nes 20

或者,基于正则表达式的解决方案:

t1 <- c('this.text', 'next.text')

# capitalize first letter
t2 <- sub('^(\\w?)', '\\U\\1', t1, perl=T)

# remove points and capitalize following letter
gsub('\\.(\\w?)', '\\U\\1', t2, perl=T)
[1] "ThisText" "NextText"
Run Code Online (Sandbox Code Playgroud)

编辑:一些解释

sub('^(\\w?)', '\\U\\1', t1, perl=T),sub这就足够了因为我们只对第一场比赛感兴趣.然后在每个字符串的开头匹配第一个字母数字字符^(\\w?).在函数的替换部分中需要括号用于反向引用.因为替换\\U用于将随后出现的所有内容(这是第一个字符)大写.

同样的原则适用于gsub('\\.(\\w?)', '\\U\\1', t2, perl=T)唯一的区别,即不匹配第一个字符,而是每个字符匹配..

  • 或者,如果使用前瞻断言,则可以在一行中执行:`gsub("(?:(?= \\ b)| \\.)([[:alpha:]])","\\ U \\ 1",t1,perl = TRUE)` (2认同)
  • 实际上,我可以进一步简化为不需要前瞻:`gsub("(?:\\ b | \\.)([[:alpha:]])","\\ U \\ 1",t1,的perl = TRUE)`.可悲的是,@ TylerRinker,速度提升可以忽略不计. (2认同)

Tyl*_*ker 16

这是一种方法,但有正则表达式可能更好的方法:

t1 <- c('this.text', 'next.text')

camel <- function(x){ #function for camel case
    capit <- function(x) paste0(toupper(substring(x, 1, 1)), substring(x, 2, nchar(x)))
    sapply(strsplit(x, "\\."), function(x) paste(capit(x), collapse=""))
}

camel(t1)
Run Code Online (Sandbox Code Playgroud)

这会产生:

> camel(t1)
[1] "ThisText" "NextText"
Run Code Online (Sandbox Code Playgroud)

编辑: 作为好奇心,我微观标记了4个答案(TOM =原始海报,TR =我自己,JMS = jmsigner&SB = sebastion;评论jmsigner的帖子),发现非正则表达式的答案更快.我会认为它们更慢.

   expr     min      lq  median      uq      max
1 JMS() 183.801 188.000 197.796 201.762  349.409
2  SB()  93.767  97.965 101.697 104.963  147.881
3 TOM()  75.107  82.105  85.370  89.102 1539.917
4  TR()  70.442  76.507  79.772  83.037  139.484
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Art*_*sov 9

tocamelrapportools包做你想要的:

> library(rapportools)
> example(tocamel)

tocaml> tocamel("foo.bar")
tocaml>     ## [1] "fooBar"
tocaml> 
tocaml>     tocamel("foo.bar", upper = TRUE)
tocaml>     ## [1] "FooBar"
tocaml> 
tocaml>     tocamel(c("foobar", "foo.bar", "camel_case", "a.b.c.d"))
tocaml>     ## [1] "foobar"    "fooBar"    "camelCase" "aBCD"
tocaml> 
Run Code Online (Sandbox Code Playgroud)

更新:

另一个简单快速的解决方案(如@rengis):

camel2 <- function(x) {
    gsub("(^|[^[:alnum:]])([[:alnum:]])", "\\U\\2", x, perl = TRUE)
}
camel2(t1)
#> [1] "ThisText" "NextText"
Run Code Online (Sandbox Code Playgroud)

与@TylerRinker解决方案比较:

identical(camel(t1), camel2(t1))
#> [1] TRUE
microbenchmark::microbenchmark(camel(t1), camel2(t1))
#> Unit: microseconds
#>        expr    min      lq     mean  median      uq     max neval cld
#>   camel(t1) 76.378 79.6520 82.21509 81.5065 82.7095 151.867   100   b
#>  camel2(t1) 15.864 16.9425 19.76000 20.9690 21.9735  38.246   100  a
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是`tocamel`函数在其名称中不使用camel-casing (7认同)

Tom*_*rot 8

实际上我想我只是从toupper的帮助文件中解决了这个问题:

camel <- function(x) {     
     s <- strsplit(x, "\\.")[[1]]     
     paste(toupper(substring(s, 1,1)), substring(s, 2),           
     sep="", collapse="") 
 }    

camel(t1) 
sapply(t1,camel)  
this.text  next.text  
"ThisText" "NextText"  
Run Code Online (Sandbox Code Playgroud)


Taz*_*Taz 6

这是通过 Snakecase 包的另一个解决方案:

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

to_upper_camel_case(t1)
#> [1] "ThisText" "NextText"
Run Code Online (Sandbox Code Playgroud)

Github链接: https: //github.com/Tazinho/snakecase


Gor*_*rka 6

janitormake_clean_names()包中的函数 有一个可以用于此目的的函数。

\n

在你的情况下:

\n
t1 <- c(\'this.text\', \'next.text\')\njanitor::make_clean_names(t1, case = "big_camel")\n#> [1] "ThisText" "NextText"\n
Run Code Online (Sandbox Code Playgroud)\n

该参数case可以是多个参数之一:

\n
\xe2\x80\x9csnake\xe2\x80\x9d, \xe2\x80\x9csmall_camel\xe2\x80\x9d, \xe2\x80\x9cbig_camel\xe2\x80\x9d, \xe2\x80\x9cscreaming_snake\xe2\x80\x9d, \xe2\x80\x9cparsed\xe2\x80\x9d, \xe2\x80\x9cmixed\xe2\x80\x9d, \xe2\x80\x9clower_upper\xe2\x80\x9d, \xe2\x80\x9cupper_lower\xe2\x80\x9d, \xe2\x80\x9cswap\xe2\x80\x9d, \xe2\x80\x9call_caps\xe2\x80\x9d, \xe2\x80\x9clower_camel\xe2\x80\x9d, \xe2\x80\x9cupper_camel\xe2\x80\x9d, \xe2\x80\x9cinternal_parsing\xe2\x80\x9d, \xe2\x80\x9cnone\xe2\x80\x9d, \xe2\x80\x9cflip\xe2\x80\x9d, \xe2\x80\x9csentence\xe2\x80\x9d, \xe2\x80\x9crandom\xe2\x80\x9d, \xe2\x80\x9ctitle\xe2\x80\x9d\n
Run Code Online (Sandbox Code Playgroud)\n

由reprex 包于 2021 年 10 月 13 日创建(v2.0.1)

\n