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.
Here's one approach but with regex there's probably better ones:
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)
This yields:
> camel(t1)
[1] "ThisText" "NextText"
EDIT: As a curiosity I microbenchmarked the 4 answers (TOM=original poster, TR=myself, JMS=jmsigner & SB=sebastion; commented on jmsigner's post) and found the non regex answers to be faster. I would have assumed them slower.
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