How to convert not.camel.case to CamelCase in R

Viewed 8768

In R, I want to convert

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

to

'ThisText' 'NextText'

I have tried

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

But this gives me

"thisext" "nextext"

as it does not replace the letter after the period.

Probably really easy but I can't work it out.

6 Answers

The make_clean_names() function in the janitor package has a function that can be used for this.

In your case:

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

The parameter case can be one of many:

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

Created on 2021-10-13 by the reprex package (v2.0.1)

Related