25

在 R 中,我想转换

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

'ThisText' 'NextText'

我试过了

gsub('\\..', '', t1)

但这给了我

"thisext" "nextext"

因为它不会替换句号之后的字母。

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

4

6 回答 6

23

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

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"

编辑:一些解释

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

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

于 2012-07-26T15:39:42.027 回答
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)

这产生:

> camel(t1)
[1] "ThisText" "NextText"

编辑: 出于好奇,我对 4 个答案进行了微基准测试(TOM=original poster,TR=myself,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

在此处输入图像描述

于 2012-07-26T15:07:12.793 回答
9

实际上我想我只是从 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"  
于 2012-07-26T15:07:02.937 回答
9

tocamelfrom rapportoolspackage 做你想做的事:

> 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> 

更新:

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

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

与@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
于 2014-11-29T19:53:13.060 回答
5

这里通过蛇盒包的另一个解决方案:

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

to_upper_camel_case(t1)
#> [1] "ThisText" "NextText"

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

于 2017-03-25T22:01:03.133 回答
2

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

在你的情况下:

t1 <- c('this.text', 'next.text')
janitor::make_clean_names(t1, case = "big_camel")
#> [1] "ThisText" "NextText"

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

“snake”, “small_camel”, “big_camel”, “screaming_snake”, “parsed”, “mixed”, “lower_upper”, “upper_lower”, “swap”, “all_caps”, “lower_camel”, “upper_camel”, “internal_parsing”, “none”, “flip”, “sentence”, “random”, “title”

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

于 2021-10-13T10:19:59.937 回答